Usage of Userdefaults in iOS (store and invoke settings information)

Source: Internet
Author: User

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)

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.