1, suitable for storing lightweight local data.
2. Nsuserdefaults supported data formats are: NSNumber, NSData, NSDate, NSString, Nsarray, Nsdictionary, BOOL
3. Store in the form of key-value pairs
4, storage time for the overall application storage time, if you want to delete, need to delete the whole application
Initialize a userdefaults
Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults]; [Defaults setobject:<# (ID) #> forkey:<# (NSString *) #>]; [Defaults setbool:<# (BOOL) #> forkey:<# (NSString *) #>];
Delete
[Defaults removeobjectforkey:<# (NSString *) #>];
// synthetic access, storing data on disk
[Defaults synchronize];
// judgment is not the first time this program is run
if (![ Defaults objectforkey:@ "First"]) { NSLog (@ "Initial Entry"); _label.text = @ "first entry"; [Defaults setbool:yes forkey:@ "First"]; } else{ NSLog (@ "Not the first time"); _label.text = @ "is not the first time to enter"; }
// print the app Resource directory path
NSLog (@ "%@", [[NSBundle Mainbundle] bundlepath]);
Make Current View end edit (text box)
[Self.view Endediting:yes];
Important: Nsuserdefaults is very useful, does not require the user to set Nsuserdefaults global variables in the program, where to use the nsuserdefaults data, then where to create a Nsuserdefaults object, Then read or write the operation.
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.
Usage of Userdefaults in iOS (store and invoke settings information)