Usage of nsuserults ults in ios
Usage of nsuserults ults in ios
The NSUserDefaults class provides a programming interface for interacting with the default system. The NSUserDefaults object is used to save and restore preference settings and configuration data related to the application. By default, the system allows applications to customize their behaviors to suit users' preferences. You can read the program settings from the user's default database when the program is running. At the same time, the NSUserDefaults cache avoids enabling the user's default database every time the data is read. You can call the synchronize method to synchronize the cache in the memory with the user's default system.
The NSUserDefaults class provides a very convenient way to obtain common types, such as floats, doubles, intergers, Booleans, and URLs. Therefore, an NSUserDefaults object must be an Attribute Table, which means we can store instances such as NSData, NSString, NSNUmber, NSDate, NSArray, and NSDictionary. If you want to store other types of objects, You need to archive them and create an NSData for storage.
The value returned from NSUserDefaults cannot be changed, even if you are using a variable value during storage. For example, if you use mutable string as the value of "MyStringDefault", the value obtained by using the stringForKey: method is still unchangeable.
NSUserDefaults is a singleton and thread-safe
When using NSUserDefaults,
Let's take a look at the following code.
NSDictionary * defaults = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
NSLog (@ Defaults: % @, defaults );
Is used to get all nsuserults ults settings on the device.
NSUserDefaults is suitable for storing lightweight local data, such as the user name and password of a login interface. In my opinion, NSUserDefaults is the first choice. You can directly read the last login information from NSUserDefaults at the next login.
If you use a self-built plist file or something, you have to display the created file and read the file. It is very troublesome. Instead, you can use NSUserDefaults to read the file, just like reading a string, you can simply read it. NSUserDefaults is easy to read.
There are multiple methods for creating a user ults, which is the simplest and fastest way to create a user defaults:
[Plain]
- NSUserDefaults * accountDefaults = [NSUserDefaults standardUserDefaults];
Add data to user defaults:
[Plain]
- [AccountDefaults setObject: nameField. text forKey: UserDefaultNameKey];
You can also add basic data types such as int, float, and bool.
[Plain]
- [AccountDefaults setBool: YES forKey: userdefaboboolkey];
Get data from user defaults:
[Plain]
- [AccountDefaults objectForKey: NCUserDefaultNameKey] [accountDefaults boolForKey: UserDefaultBoolKey];
Key points:
Nsuserults ults is very easy to use. You do not need to set the global variable of NSUserDefaults in the program. Where to use NSUserDefaults data, create an NSUserDefaults object and perform read or write operations.
You can rewrite the object or data corresponding to the same keyword. After rewriting, the keyword corresponds to a new object or data, and the old object or data is automatically cleared.