下次我打开应用程序时,Swift Filled plist 是空的

标签 swift core-data plist

当我第一次打开我的应用程序时,我有一个空的 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


https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

关于下次我打开应用程序时,Swift Filled plist 是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30741665/

相关文章:

swift - 为什么将 UIView 重用为两个 UITextField 的 .leftView 会导致大量内存使用

ios - 当管理对象上下文从持久性存储中加载数据时

iphone - 更改多行时使用 NSFetchedResultsController 禁用 UITableView 的动画

xcode - 处理 Xcode 构建阶段 Bash 运行脚本中的空格和特殊字符

ios - 将动态内容写入 xCode 项目 .plist

swift - watch 操作系统 : Swift KVO for BatteryState is not working

swift - 为什么 SKSpriteNode 不能与 SKAction 一起使用?

ios - 将类型别名作为类类型存储在 NSHashTable 中

arrays - 如何在 Core Data 中存储一组自定义(和重复)对象?

ios - 从 plist 加载字符串到 UILabel (Swift)