How iOS app data is stored (preferences)
First, Brief introduction
Many iOS apps support preferences such as saving usernames, passwords, font size, and more, and iOS offers a standard set of solutions to add preferences to your app
Each app has a Nsuserdefaults instance that accesses preferences. For example, save the user name, the font size, whether to log on automatically
Storage location:
Storage format:
Second, code example
1.storyboard
2. Code
8 9 #import "YYViewController.h" 10//Preferences @interface Yyviewcontroller () 12/**13 * Save Data */15-(ibaction) SaveData: ( ID) sender;16/**17 * Read Data */19-(ibaction) ReadData: (ID) sender;20 @end22 @implementation YYViewController24 25 -(Ibaction) SaveData: (ID) Sender {26//1. Get Nsuserdefaults objects Nsuserdefaults *defaults=[nsuserdefaults standarduser DEFAULTS];28 29//2 Save data (if no synchronization occurs after setting data, the data is automatically saved to the Preferences folder at a later point in time) [Defaults setobject:@ "Yangyong" for key:@ "Name"];31 [Defaults setinteger:23 forkey:@ "Age"];32 [defaults setdouble:1.73f forkey:@ "height"];33 [DEFA Ults setobject:@ "man" forkey:@ "gender"];34 35//3. Forcing data to be saved immediately [defaults synchronize];37}38-(ibaction) Rea DData: (ID) Sender {40//1. Get Nsuserdefaults object Nsuserdefaults *defaults=[nsuserdefaults standarduserdefaults];42 Read saved data nsstring *name=[defaults objectforkey:@ "name"];44 nsstring *gender=[defaults objectforkey:@ "gender"] ; Nsinteger Age=[dEfaults integerforkey:@ "Age"];46 double height=[defaults doubleforkey:@ "height"];47//Print Data NSLog (@ "Name=%@,g ender=%@,age=%d,height=%.1f ", name,gender,age,height);}50 @end
3. Click on save data, read the data button to print as follows
Iii. Additional Information
1. Save your data
1. Get Nsuserdefaults object nsuserdefaults *defaults=[nsuserdefaults standarduserdefaults]; 2 Save Data [Defaults setobject:@ "Yangyong" forkey:@ "name"]; [Defaults setinteger:23 forkey:@ "age"]; [Defaults setdouble:1.73f forkey:@ "height"]; [Defaults setobject:@ "Man" forkey:@ "gender"]; 3. Force the data to be saved immediately [defaults synchronize];
2. Reading data
1. Get Nsuserdefaults object nsuserdefaults *defaults=[nsuserdefaults standarduserdefaults]; 2. Read the saved data nsstring *name=[defaults objectforkey:@ "name"]; NSString *gender=[defaults objectforkey:@ "gender"]; Nsinteger age=[defaults integerforkey:@ "age"]; Double height=[defaults doubleforkey:@ "height"];
3. Important Notes
(1) Preferences are specifically used to save configuration information for an application, and generally do not save other data in preferences. If you use System Preferences to store data, the default is to store it under the Preferences folder, and the preferences will save all the data in the same file.
(2) After saving the data by using the preferences, the time it saves to the system is indeterminate, the data will be saved to the Preferences folder automatically at a certain point in the future, and if the data is stored immediately, you can use [defaults synchronize];
(3) Note: All the information is written in a single file, compared to simple plist can save and read the basic data types.
(4) Step: Get Nsuserdefaults, Save (read) data
How iOS app data is stored (preferences)