In doubt, please add QQ Exchange Group: 390438081
My qq:604886384 (indicated)
: niuting823
First of all, what is data persistence technology?
NSString *str = @ "Hello world"; Memory
It's actually storing the data on your hard disk, which is stored in a sandbox for iOS apps.
. Nsuserdefault Introduction
1 is a system-provided custom class that can be used at any time to declare the object, and whenever necessary to remove the object
2 Supported Object types: NSString, NSNumber, NSData, Nsarray, Nsdictionary, BOOL, Nsinteger, nsfloat;
3 custom object types are not supported
. Nsuserdefault characteristics
1 when you add data to Nsuserdefault, it becomes a global variable that reads and writes Nsuserdefault data in the entire app.
2 Repeatable Read/write: For objects that correspond to the same keyword, you can repeat the write.
3 * Note: The*nsuserdefault is timed to write data to disk/hard disk, rather than write in a timely manner; If you need to write to disk at any time, you need to call a synchronous function synchronize, which forces the disk to be written immediately. (for special attention)
. In the end Nsuserdefault persisted data, deposit where????
first step in storyboard drag two textfiled controls, name and age
The second step declares the name and age attribute
The third step is to customize a method named Initdatabyuserdefault
Fourth step after the user has removed the daemon, you need to know the trigger time/method (notification) for this action
Fifth Step implementation Trigger method
-(void) Applicationwilldidenterbackground: ( nsnotification *) Notification {
NSLog(@ " program starts to go backstage ." ) );
//4. in the trigger method, save the data you want to save.
nsuserdefaults *defaults = [nsuserdefaultsstandarduserdefaults ];
// use defaults to save name / Age text for user data
[Defaultssetobject:self. Nametextfield . text forkey:@ "name"];
[Defaultssetobject:self. Agetextfield . text forkey:@ "age"];
//force user-entered name and age data to be saved to the hard Drive
[Defaultssynchronize];
}
iOS Development Data Persistence Technology 01--nsuserdefault Introduction