当我第一次打开我的应用程序时,我有一个空的 plist
,我用一些值 (writeToFile
) 填充了它。我可以动态地从 plist
中读取并获取我刚刚放入其中的那些值。到目前为止这么好。然后我关闭我的应用程序,稍后我再次打开它。此时我希望我的 plist
包含我上次打开应用程序时动态写入的那些值,但对我不利的是它是空的。
这是正常行为吗?我不能将 plist
用作动态保存值的“本地数据库”,并在其他时间打开应用程序时读取它们吗?
谢谢!
最佳答案
不应修改“支持文件”中的文件。
支持文件组的文件(与所有其他非代码文件一样)被复制到应用程序包中。 Apple 对 app bundle 的评价:
This directory contains the app and all of its resources. You cannot write to this directory. [...] Writing to this directory changes the signature and prevents your app from launching. You can, however, gain read-only access to any resources stored in the apps bundle.
如果你想存储用户偏好、选择等,你应该在文档目录中创建 plist 文件。
Use this directory to store user-generated content. The contents of this directory can be made available to the user through file sharing; therefore, his directory should only contain files that you may wish to expose to the user. The contents of this directory are backed up by iTunes
您可以使用以下方法获取文档目录的路径:
NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last as! String
如果您想存储首选项,您可能还想看看 NSUserDefaults
。
关于下次我打开应用程序时,Swift Filled plist 是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30741665/