iOS Development Data Persistence Technology 01--nsuserdefault Introduction

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.