iOS Development UI Chapter-ios Application Data Storage (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
1 //2 //YYVIEWCONTROLLER.M3 //01-Preference Settings4 //5 //Created by Apple on 14-6-7.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten //Preference Settings One @interfaceYyviewcontroller () A /** - * Save Data - */ the-(Ibaction) SaveData: (ID) sender; - /** - * Read Data - */ +-(Ibaction) ReadData: (ID) sender; - + @end A at @implementationYyviewcontroller - --(Ibaction) SaveData: (ID) Sender { - //1. Get the Nsuserdefaults object -Nsuserdefaults *defaults=[Nsuserdefaults standarduserdefaults]; - in //2 Save data (if no synchronization occurs after setting up the data, the data is automatically saved to the Preferences folder at a future point in time) -[Defaults setobject:@"Yangyong"Forkey:@"name"]; to[Defaults Setinteger: atForkey:@" Age"]; +[Defaults setdouble:1.73fForkey:@"Height"]; -[Defaults setobject:@"Mans"Forkey:@"Gender"]; the * //3. Force data to be saved immediately $ [Defaults synchronize];Panax Notoginseng } - the-(Ibaction) ReadData: (ID) Sender { + //1. Get the Nsuserdefaults object ANsuserdefaults *defaults=[Nsuserdefaults standarduserdefaults]; the //read the saved data +NSString *name=[defaults Objectforkey:@"name"]; -NSString *gender=[defaults Objectforkey:@"Gender"]; $Nsinteger age=[defaults Integerforkey:@" Age"]; $ DoubleHeight=[defaults Doubleforkey:@"Height"]; - //Print Data -NSLog (@"name=%@,gender=%@,age=%d,height=%.1f", name,gender,age,height); the } - @end
3. Click on save data, read the data button to print as follows
Iii. Additional Information
1. Save your data
1 //1. Get the Nsuserdefaults object2Nsuserdefaults *defaults=[Nsuserdefaults standarduserdefaults];3 4 //2 Saving Data5[Defaults setobject:@"Yangyong"Forkey:@"name"];6[Defaults Setinteger: atForkey:@" Age"];7[Defaults setdouble:1.73fForkey:@"Height"];8[Defaults setobject:@"Mans"Forkey:@"Gender"];9 Ten //3. Force data to be saved immediately One[Defaults synchronize];
2. Reading data
1 //1. Get the Nsuserdefaults object2Nsuserdefaults *defaults=[Nsuserdefaults standarduserdefaults];3 //2. Read the saved data4NSString *name=[defaults Objectforkey:@"name"];5NSString *gender=[defaults Objectforkey:@"Gender"];6Nsinteger age=[defaults Integerforkey:@" Age"];7 DoubleHeight=[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
iOS Development UI Chapter-ios Application Data Storage (preferences)