简体   繁体   English

如何使用一行代码在Swift 2.0中读取文本文件

[英]How to read text-file in Swift 2.0 with one line of code

I'm trying to read the contents of a file. 我正在尝试读取文件的内容。

In Python, it would be a one liner: 在Python中,它将是一个内衬:

seq_file = open('/some/local/file-path/on/computer.txt','r').read()
print seq_file

How can you write a one-liner in Swift to do the same thing? 如何在Swift中编写单行代码以完成相同的工作?

My swift code to open a file (only works in terminal not in Playground): 我的快速代码打开了一个文件(仅适用于终端,不适用于Playground):

let seq_path = "/some/local/file-path/on/computer.txt"
let file_manager = NSFileManager.defaultManager()
let data: NSData = file_manager.contentsAtPath(seq_path)!
let seq_file = NSString(data: data, encoding: NSUTF8StringEncoding)
print(seq_file)

I'm not sure why reading is so much more complicated in Swift. 我不确定为什么在Swift中阅读如此复杂。 If someone would like to explain that would be extremely helpful. 如果有人想解释,那将非常有帮助。
I've read about the advantages of Swift...If it's so new and powerful, why would they make such a simple task so complicated? 我已经读过Swift的优势...如果它是如此新颖和强大,为什么他们会将如此简单的任务变得如此复杂?

Background (if anyone is interested): Undergrad in Biology, started learning Python in 2012 as my first programming language. 背景知识(如果有兴趣的话):生物学本科生,从2012年开始学习Python,这是我的第一门编程语言。 Use it to analyze biological data to this day. 用它来分析今天的生物学数据。 Now I'm a graduate student and I've been getting a lot more interested in Computer Science over the years. 现在我是一名研究生,多年来我对计算机科学越来越感兴趣。 I would like to start writing programs for OSX and iOS and I thought a good way to learn the language was rewrite all my algorithms in Swift. 我想开始为OSX和iOS编写程序,我认为学习该语言的一种好方法是在Swift中重写所有算法。

Dunno, I suppose language designers have their priorities. Dunno,我想语言设计师应该优先考虑。 Charitably, the swift code could lose a line and still read well: 慈善地说,快速的代码可能会丢失一行,但仍能很好地读取:

let file_manager = NSFileManager.defaultManager()
let data: NSData = file_manager.contentsAtPath("/some/local/file-path/on/computer.txt")!
let seq_file = NSString(data: data, encoding: NSUTF8StringEncoding)
print(seq_file)

This is more or less what it took to read a file with java 1.4: 这或多或少是使用Java 1.4读取文件的过程:

try
{
    String line;
    File file = new File(("manifest.mf");
    BufferedReader inFile = new BufferedReader(new FileReader(file));
    while((line = inFile.readLine()) != null)
    {
            System.out.println(line)
    }
    inFile.close();
}
catch (IOException e)
{
    System.out.println("problem with file");
}

In java 8, you can do something like this: 在Java 8中,您可以执行以下操作:

    Files.lines(Paths.get("manifest.mf")).forEach(System.out::println);

Things change. 事情会改变的。 Swift is at 1.2. 斯威夫特是1.2。 It does seem a bit old fashioned though. 虽然看起来确实有些过时。

I've read about the advantages of Swift...If it's so new and powerful, why would they make such a simple task so complicated? 我已经读过Swift的优势...如果它是如此新颖和强大,为什么他们会将如此简单的任务变得如此复杂?

Well, in Swift you can do this: 好吧,在Swift中,您可以执行以下操作:

let string = try? NSString(contentsOfFile: "name.txt", encoding: NSASCIIStringEncoding)
print(string)

The question is if you really want to, because the longer versions give you more flexibility and better error handling... 问题是您是否真的想要,因为较长的版本可以为您提供更大的灵活性和更好的错误处理能力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM