iOS nsuserdefaults store data (preferences)
The 1.NSUserDefaults is used to store data with small amounts of data, primarily user-configurable, but can also support storing some small data
Includes:nsstring, NSNumber, NSDate, Nsarray, Nsdictionary
2. The data is stored in a plist file. This file is located in the/library/preferences directory of the app sibling directory
3.NSUserDefaults is mainly setobject and object Forkey, big data use other storage, such as sqlite3,coredate, etc.;
4. After saving the data using the preferences, the time it saves to the system is indeterminate, the data will be saved to the Preferences folder automatically at a certain point in the future, and if you need to store the data immediately, you can use [defaults synchronize];
1. plist file storage address for preference settings
NSString *path = [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastObject]; NSLog (@ "%@", Path);
2. Read deposit NSString (nsnumber,nsdate)
Nsuserdefaults *defaults =[Nsuserdefaults Standarduserdefaults]; //Write Data[Defaults setobject:@"teststring"Forkey:@"test1"]; //Write Now[Defaults synchronize]; //reading DataNSString *STR = [Defaults objectforkey:@"test1"]; NSLog (@"---%@", str);
3. Read Deposit Nsarray,nsdictionary
Nsarray,nsdictionary are immutable types and are similar.
However, if the deposit is nsmutablearray,nsmutabledictionary, the read-out array is immutable in the type: Nscfarray. needs to be transformed into Nsmutablearray.
Nsuserdefaults *defaults =[Nsuserdefaults Standarduserdefaults]; Nsmutablearray*array = [Nsmutablearray arraywithobjects:@"test1",@"test2",@"test3", nil]; [Array AddObject:@"test4"]; //Write Data[Defaults Setobject:array Forkey:@"test2"]; //Write Now[Defaults synchronize]; //reading Data//reason: '-[__nscfarray insertobject:atindex:]: Mutating method sent to immutable object ' this directly equals the word mutablearray type NS Cfarray, cannot continue to add to the array//Nsmutablearray *mutablearray = [Defaults objectforkey:@ "Test2"];Nsmutablearray *mutablearray = [Nsmutablearray arraywitharray:[defaults objectforkey:@"test2"]]; [Mutablearray AddObject:@"Test5"]; NSLog (@"%@", Mutablearray);
iOS nsuserdefaults store data (preferences)