Swift - 向/从文件读/写数组数组

ati*_*rit 4 arrays read-write swift

是我的文件.

我该怎么写这个数组:

var listOfTasks = [["Hi", "Hello", "12:00"],["Hey there", "What's up?", "3:17"]]

.txt文件(file.txt)?我知道还有其他问题,但它们是其他语言(不是Swift).我希望它可以在真正的iPhone上运行.如有必要,我很乐意提供更多信息.我是Swift的新手,请原谅我的问题,如果它看起来太简单了.注意:我特别询问有关包含数组的数组.

提前致谢!

red*_*t84 6

您的其他问题中已经回答:

这是一个完整的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)
Run Code Online (Sandbox Code Playgroud)