Here是我的文件。
我怎样才能写这个数组:
var listOfTasks = [["Hi", "Hello", "12:00"],["Hey there", "What's up?", "3:17"]]
到 .txt
文件 (file.txt
)?我知道还有其他关于此的问题,但它们使用其他语言(不是 Swift)。我希望它能在实际的 iPhone 上运行。如有必要,我很乐意提供更多信息。我是 Swift 的新手,所以请原谅我的问题,如果它看起来太简单了。 注意:我特别询问包含数组的数组。
提前致谢!
最佳答案
已经answered you在 your other question :
这是一个完整的 Playground 示例:
let fileUrl = NSURL(fileURLWithPath: "/tmp/foo.plist") // Your path here
let listOfTasks = [["Hi", "Hello", "12:00"], ["Hey there", "What's up?", "3:17"]]
// Save to file
(listOfTasks as NSArray).writeToURL(fileUrl, atomically: true)
// Read from file
let savedArray = NSArray(contentsOfURL: fileUrl) as! [[String]]
print(savedArray)
关于arrays - Swift - 从文件读取/写入数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37599632/