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 //Created by Xin on 14-10-24.2 //Copyright (c) 2014 Liang. All rights reserved.3 //4 5 #import "TXViewController.h"6 7 @interfaceTxviewcontroller ()8-(ibaction) Save;9-(ibaction) read;Ten One @end A - @implementationTxviewcontroller - the- (void) Viewdidload - { - [Super Viewdidload]; - //additional setup after loading the view, typically from a nib. + } - +- (void) didreceivememorywarning A { at [Super didreceivememorywarning]; - //Dispose of any resources the can be recreated. - } - --(ibaction) Save { - //is a single-instance method that returns 1. Direct access to Software preferences (library/preferences) with Nsuserdefaults inNsuserdefaults *defaulta =[Nsuserdefaults standarduserdefaults]; - to //2. Storing data What he's been preaching is a healthy value. +[Defaulta SetObject:@"JSLFSL"Forkey:@"name"]; -[Defaulta SetObject:@"123"Forkey:@"Paw"]; the[Defaulta Setinteger: OneForkey:@" Age"]; *[Defaulta setbool:yes Forkey:@"BOOL"]; $ Panax Notoginseng //3. Sync now - [Defaulta synchronize]; the } + A-(ibaction) Read { theNsuserdefaults *defaults =[Nsuserdefaults standarduserdefaults]; + -NSString *name = [Defaults objectforkey:@"name"]; $BOOL booll = [Defaults boolforkey:@"BOOL"]; $NSLog (@"%@%d", name,booll); - - } the @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)