Swift-从文件读取/写入数组的数组 [英] Swift - Read/write array of arrays to/from file

查看:373
本文介绍了Swift-从文件读取/写入数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的文件.

如何编写此数组:

var listOfTasks = [["Hi","Hello","12:00"],[嘿,",怎么了?","3:17"]]

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

to a .txt file (file.txt)? I know there are other questions about this, but they're in other languages (not Swift). I'd like for it to work on an actual iPhone. I'm happy to provide additional information if necessary. I'm new to Swift, so excuse my question if it seems too simple. NOTE: I am asking specifically about arrays containing arrays.

提前谢谢!

推荐答案

已经这是一个完整的Playground示例:

Here's a full working Playground sample:

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)

这篇关于Swift-从文件读取/写入数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆