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
////YYVIEWCONTROLLER.M//01-Preference Settings////Created by Apple on 14-6-7.//Copyright (c) 2014 itcase. All rights reserved.//#import"YYViewController.h"//Preference Settings@interface Yyviewcontroller ()/** * Save Data*/-(ibaction) SaveData: (ID) sender;/** * Read Data*/-(ibaction) ReadData: (ID) sender; @end @implementation Yyviewcontroller-(ibaction) SaveData: (ID) Sender {//1. Get the Nsuserdefaults objectNsuserdefaults *defaults=[Nsuserdefaults Standarduserdefaults]; //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"]; [Defaults Setinteger: atForkey:@" Age"]; [Defaults setdouble:1.73fForkey:@"Height"]; [Defaults setobject:@"Mans"Forkey:@"Gender"]; //3. Force data to be saved immediately[Defaults synchronize];}-(ibaction) ReadData: (ID) Sender {//1. Get the Nsuserdefaults objectNsuserdefaults *defaults=[Nsuserdefaults Standarduserdefaults]; //read the saved dataNSString *name=[defaults Objectforkey:@"name"]; NSString*gender=[defaults Objectforkey:@"Gender"]; Nsinteger Age=[defaults Integerforkey:@" Age"]; DoubleHeight=[defaults Doubleforkey:@"Height"]; //Print DataNSLog (@"name=%@,gender=%@,age=%d,height=%.1f", name,gender,age,height);} @end
3. Click on save data, read the data button to print as follows
Iii. Additional Information
1. Save your data
//1. Get the Nsuserdefaults objectNsuserdefaults *defaults=[Nsuserdefaults Standarduserdefaults]; //2 Saving Data[Defaults setobject:@"Yangyong"Forkey:@"name"]; [Defaults Setinteger: atForkey:@" Age"]; [Defaults setdouble:1.73fForkey:@"Height"]; [Defaults setobject:@"Mans"Forkey:@"Gender"]; //3. Force data to be saved immediately[Defaults synchronize];
2. Reading data
//1. Get the Nsuserdefaults objectNsuserdefaults *defaults=[Nsuserdefaults Standarduserdefaults]; //2. Read the saved dataNSString *name=[defaults Objectforkey:@"name"]; NSString*gender=[defaults Objectforkey:@"Gender"]; Nsinteger Age=[defaults Integerforkey:@" Age"]; 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)