synchronize
方法,把内存中的数据同步到磁盘中去。在程序退出时,一定要调用这个方法。[NSUserDefaults standardUserDefaults]
管理的内容,对应的实体文件存储在$HOME/Library/Preferences/
目录下,名字叫做<ApplicationBundleIdentifer>.plist
,监听默认值的变化
NSUserDefaultsDidChangeNotification
This notification is posted when a change is made to defaults in a persistent domain.
可以存储的类型
A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary
持久化首选项实际上就是把要存储的内容encode到磁盘里,下次启动时在decode里。
这就是说,新版本必须可以decode旧版本的encode的内容,而这是很容易出错的,因此不要存储复杂的东西。最好是存储简单的变量,然后根据这些个变量来生成复杂的对象。
原文:http://www.cnblogs.com/huahuahu/p/NSUserdefault-du-shu-bi-ji.html