[OC learning-30] nsuserdefaults stores data locally.

Source: Internet
Author: User

Create an nsuserdefaults object (similar to a dictionary), write the data to the object, and save the data in the object locally. Then we delete the code and re-create an object to call the local data to see if the data can be called up. If yes, the data has been localized.


(1) Create an empty application.


(2) Delete all the functions under appdelegate. M. Add the following code to the first function and click Run.

# Import "appdelegate. H "@ implementation appdelegate-(bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {self. window = [[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds]; // override point for customization after application launch. self. window. backgroundcolor = [uicolor whitecolor]; [self. window makekeyandvisible]; // the newly added code nsarray * [email protected] [@ "111", @ "222"]; nsuserdefaults * userdef = [nsuserdefaults standarduserdefaults] is shown below; // This object is similar to a dictionary. It is also a singleton example [userdef setobject: arr1 forkey: @ "array"]; [userdef setinteger: 123 forkey: @ "Number"]; [userdef synchronize]; // synchronize data to the local return yes;} @ end

(3) Delete the above Code, add the following code, and click Run.

# Import "appdelegate. H "@ implementation appdelegate-(bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {self. window = [[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds]; // override point for customization after application launch. self. window. backgroundcolor = [uicolor whitecolor]; [self. window makekeyandvisible]; // the following code is added. // check whether the data is local. That is, cancel the preceding statement and call nsuserdefaults * userdefault = [nsuserdefaults standarduserdefaults] locally. nsinteger * num1 = [userdefault integerforkey: @ "Number"]; nsarray * arr2 = [userdefault objectforkey: @ "array"]; nslog (@ "% d, % @", num1, arr2); Return yes;} @ end

(4) running result:

123,(    111,    222)

After the code added for the first time has been deleted, the data can be called up during the second code run, indicating that the data is stored locally.

(5) In fact, this data should be stored in one. it is a pity that the file is not found or found, but the data is not seen, or the source file has been saved, there may be no need to create a new file to save it, so you can find it later when you are working on the project.

[OC learning-30] nsuserdefaults stores data locally.

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.