Overview
The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences. For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved. Applications record such preferences by assigning values to a set of parameters in a user’s defaults database. The parameters are referred to as defaults since they’re commonly used to determine an application’s default state at startup or the way it acts by default.
At runtime, you use anNSUserDefaults object to read the defaults that your application uses from a user’s defaults database.NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value. Thesynchronize method, which is automatically invoked at periodic intervals, keeps the in-memory cache in sync with a user’s defaults database.
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. 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, orNSDictionary. If you want to store any other type of object, you should typically archive it to create an instance ofNSData. For more details, seePreferences and Settings Programming Guide.
Values returned fromNSUserDefaults are immutable, even if you set a mutable object as the value. For example, if you set a mutable string as the value for"MyStringDefault", the string you later retrieve using stringForKey: will be immutable.
A defaults database is created automatically for each user. TheNSUserDefaults class does not currently support per-host preferences. To do this, you must use the CFPreferences API (seePreferences Utilities Reference). However, NSUserDefaults correctly reads per-host preferences, so you can safely mix CFPreferences code withNSUserDefaults code.
If your application supports managed environments, you can use anNSUserDefaults object to determine which preferences are managed by an administrator for the benefit of the user. Managed environments correspond to computer labs or classrooms where an administrator or teacher may want to configure the systems in a particular way. In these situations, the teacher can establish a set of default preferences and force those preferences on users. If a preference is managed in this manner, applications should prevent users from editing that preference by disabling any appropriate controls.
The NSUserDefaults class is thread-safe.
Tasks
Getting the Shared NSUserDefaults Instance
Initializing an NSUserDefaults Object
Registering Defaults
Getting Default Values
Setting Default Values
Removing Defaults
Maintaining Persistent Domains
Accessing Managed Environment Keys
Managing the Search List
Maintaining Volatile Domains
Maintaining Suites
翻译上面NSUserDefaults的API大概意思如下:
NSUserDefaults类提供了一个与默认系统进行交互的编程接口。NSUserDefaults对象是用来保存,恢复应用程序相关的偏好设置,配置数据等等。默认系统允许应用程序自定义它的行为去迎合用户的喜好。你可以在程序运行的时候从用户默认的数据库中读取程序的设置。同时NSUserDefaults的缓存避免了在每次读取数据时候都打开用户默认数据库的操作。可以通过调用synchronize方法来使内存中的缓存与用户默认系统进行同步。
从NSUserDefaults返回的值是不可改变的,即便是你在存储的时候使用的是可变的值。例如你使用mutable string做为“MyStringDefault”的值,当你做使用stringForKey:方法获取的值,这个值仍然是不可变的。
NSUserDefaults是单例,同时也是线程安全的.
示例代码如下:
#pragma mark---------存 //创建 NSUserDefaults 单例 NSUserDefaults * ud = [NSUserDefaults standardUserDefaults]; //把_userNameTextField.text存到 NSUserDefaults 中 [ud setObject:_userNameTextField.text forKey:@"myKey"]; [ud synchronize]; #pragma mark---------取 //从 NSUserDefaults 中取出 数据 NSString * value = [ud objectForKey:@"myKey"]; NSLog(@"value = %@",value);