与自己的 Plist 比较
在我过去的项目中,我总是制作一个 NSObject-HelperClass 来与自制的 Plist 交互。现在我听说我可以使用 [NSUserDefaults standardUserDefaults] 并且还有我的字典。
那么为什么我应该使用 UserDefaults 而不是我自己的 NSObject 来处理 plist 呢?
干杯远藤
In my past projects I always made a NSObject-HelperClass which interacts with a selfmade-Plist. Now i hear I can use [NSUserDefaults standardUserDefaults] and have also my dictionary.
So why should I use UserDefaults instead of my own NSObject which massages the plist?
cheers endo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSUserDefaults 是存储某些首选项和应用程序当前状态的便捷方法(例如:“记住我”复选框设置为是或否)。您应该仅将其用于此类数据。
您还可以使用自定义 PList 来存储此类信息,但这可能有点过头了。
关键问题是您需要存储的数据有多大?
小数据集=> NSUserDefaults
大数据集 => PList
如果您有严重的存储需求,您还应该考虑使用 CoreData。
NSUserDefaults is a convenient way to store some preferences and the current state of the application (ex: "remember me" checkbox set to yes or no). You should only use it for this kind of data.
You can also use a custom PList to store this kind of information but it can be overkill.
The key question is how large are the data you need to store ?
small dataset => NSUserDefaults
large dataset => PList
You should also consider using CoreData if you have serious storage needs.
如果我没记错的话,建议 Apple 主要使用 NSUserDefaults 作为用户默认值。也许我在 WWDC 2010 的一个视频中听到过。
NSUserDefaults 的最大问题是它们是“半不可变的”。即使您只更改该集合中包含的一个对象,您也必须用
setObjectForKey
替换整个集合。除此之外,我认为如果你同意的话,使用 NSUserDefaults 就可以了。
If I recall correctly, Apple is recommended to use NSUserDefaults primarily for user defaults. Maybe I heard it in one of the videos from WWDC 2010.
The biggest problem with NSUserDefaults is that they are 'semi-immutable'. You'll have to replace whole collection with
setObjectForKey
even if you change only one object contained in that collection.Other than that, I think it is fine to use NSUserDefaults if you're ok with it.
NSUserDefaults 有许多有用的属性;例如,它会自动合并更改以避免过于频繁地写入磁盘。您还可以使用终端中的“默认”命令来操作它,这可以方便地进行测试。最后,它是线程安全的,但很难正确做到这一点。
NSUserDefaults has a number of properties that could be useful; for example it will automatically coalesce changes to avoid writing to disk too often. Also you can manipulate it using the 'defaults' command in Terminal which can be handy for testing. Lastly, it's threadsafe which is tricky to do properly.