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.