Usage of nsuserdefaults in iOS
nsuserdefaults class provides a programmatic interface to interact with the default system. The Nsuserdefaults object is used to save, restore application-related preferences, configuration data, and so on . The default system allows the application to customize its behavior to suit the user's preferences. you can read the program's settings from the user's default database while the program is running. At the same time, the nsuserdefaults cache avoids the operation of opening the user's default database every time the data is read. You can synchronize the in-memory cache with the user's default system by calling the Synchronize method.
The Nsuserdefaults class provides a very convenient way to get common types, such as Floats,doubles,intergers,booleans,urls. So a Nsuserdefaults object must be an attribute table, which means we can store nsdata,nsstring,nsnumber,nsdate,nsarray,nsdictionary these instances. If you want to store other types of objects, you need to archive them and create a nsdata to implement the storage.
The value returned from Nsuserdefaults is immutable, even if you are storing it with a variable value. For example, you use the mutable string as the value of "Mystringdefault", and when you do use the Stringforkey: method to get the value, the value is still immutable.
Nsuserdefaults is a singleton, but also a thread-safe
When using the Nsuserdefaults,
First look at the following code
nsdictionary* defaults = [[Nsuserdefaults standarduserdefaults] dictionaryrepresentation];
NSLog (@ "Defaults:%@", Defaults);
is used to get all the devices on the The settings for the nsuserdefaults.
Nsuserdefaults is suitable for storing lightweight local data , such as the data to save a login interface, user name, password, and so on, individuals feel that using nsuserdefaults is preferred. The next time you log in, you can read the last login information directly from the Nsuserdefaults.
Because if you use the Plist file you created, you also have to display the creation of files, read files, very troublesome, but with nsuserdefaults do not care about these things, like reading a string, directly read it. Nsuserdefaults is very convenient and easy to read.
There are several ways to create a user defaults method, the simplest way to create it quickly:
[Plain]View Plaincopy
- Nsuserdefaults *accountdefaults = [Nsuserdefaults standarduserdefaults];
Add data to User defaults:
[Plain]View Plaincopy
- [Accountdefaults SetObject:nameField.text Forkey:userdefaultnamekey];
You can also add the basic data type int, float, bool, etc., have the corresponding method
[Plain]View Plaincopy
- [Accountdefaults Setbool:yes Forkey:userdefaultboolkey];
from get data in user defaults:
[Plain]View Plaincopy
- [Accountdefaults Objectforkey:ncuserdefaultnamekey]
- [Accountdefaults Boolforkey:userdefaultboolkey];
Points:
Nsuserdefaults is very useful and does not require the user to set up in the program nsuserdefaults Global variables, where to use nsuserdefaults data, then where to create a The Nsuserdefaults object is then read or written.
For the same keyword corresponding to the object or data, it can be rewritten, after rewriting the keyword corresponding to the new object or data, the old object or data will be automatically cleaned up.