1. Use: Nsuserdefaults is used to store data such as user-like configuration, program settings information , etc.
nsuserdefault, Nskeyedarchiver, SQLite, CoreData a few common ways to store data, Where Nsuserdefaults is used to store data such as a user-like configuration, both users store large quantities and more complex data.
nsuserdefault basically supports all native data types nsstring, NSNumber, NSDate, NSArray, Nsdictionary If you want to store a custom object, such as a custom class object, you must convert it to nsdata storage.
2. Usage:
The use of Nsuserdefault is relatively simple:
Category A page:
nsuserdefaults *userdefault = [nsuserdefaultsstandarduserdefaults] ;
[Userdefault setobject: @ "Beijing" forkey:lastcity];
[Userdefault synchronize];
Class B page:
nsuserdefaults *userdefault = [nsuserdefaultsstandarduserdefaults];
nsstring *lastcity = [Userdefault objectforkey:lastcity];
3. Detail usage:
If you want to store a custom object, such as a custom class object, you must convert it to nsdata storage:
[CPP]View Plaincopy
- NSArray *arr = [[nsarray alloc] initwithobjects:@ " arr1 " , @ " arr2 " , NIL]  
- [mysettingdata setobject:arr forkey:@" Arritem " ];  
- [mysettingdata setobject:@" admin " forkey:@ " user_name " ];  
- [mysettingdata setbool: @YES forkey:@" Auto_login " ];  
- [Mysettingdata setinteger:1 forkey:@"Count"];
when you add data to nsuserdefaults, they become global variables, and the app can read and write data from the Nsuserdefault:
[CPP]View Plaincopy
- Nsuserdefaults *mysettingdatar = [Nsuserdefaults standarduserdefaults];
- nslog (@" arritem=%@ " , [mysettingdatar objectforkey:@ " Arritem "
- nslog (@" user_name=%@ " , [mysettingdatar objectforkey:@ " user_name "
- NSLog (@"count=%d", [Mysettingdatar integerforkey:@"Count"]);
If you want to delete a data item, you can use Removeobjectforkey to delete the data:
[CPP]View Plaincopy
- [mysettingdata removeobjectforkey:@"Arritem"];
It is important to note that the nsuserdefaults is timed to write the data in the cache to the disk, rather than the instant write, in order to prevent the data loss caused by the program exit after writing the nsuserdefaults. You can use synchronize to force the data to be written to disk immediately after the data is written:
[CPP]View Plaincopy
- [ mysettingdata synchronize];
After you run the above statement, the data in the Nsuserdefaults is written to the. plist file, and if you run the program on the emulator, you can/users/your-username/library/application the Mac Support/iphone simulator/4.1/applications/your-app-dir/library/prefereces directory below find a file named Your-bundle_ Identifier.plist the plist file, open the file with Xcode and you can see the data you just wrote.
4, 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.
IOS data persistence using Nsuserdefaults to store data