In IOS development, there is often a need to store data locally. For example, some data cache, or record the user's account password, record the next time whether or not to log in, etc., these need to log data to local. There are three common ways in which data is stored locally in iOS:
One: Use plist to store locally;
II: Preference (Preferences)
Three: Archive the solution file.
Give an example of three ways to do this.
Before you introduce the data store, you need to understand the sandbox mechanism for apps in iOS, as detailed in: http://www.cnblogs.com/acBool/p/5467060.html.
Way One:
By using plist to store data locally, the data is actually written to a file, and the data in the file is saved in XML format. Using plist to store data is very convenient and the code is as follows:
//Get sandbox pathNSString *home =nshomedirectory (); NSLog (@"home =%@", Home); NSString*docpath = [Home stringbyappendingpathcomponent:@"Documents"]; //file pathNSString *filepath = [DocPath stringbyappendingpathcomponent:@"data.plist"]; Nsarray*data = @[@"Test",@Ten,@"Lalala"]; [Data Writetofile:filepath Atomically:yes];
Read the saved data code from the Plist file as follows:
// get sandbox path nsstring *home = Nshomedirectory (); NSString *docpath = [Home Stringbyappendingpathcomponent:@ " documents // file path nsstring *filepath = [ DocPath stringbyappendingpathcomponent:@ " *data = [Nsarray Arraywithcontentsoffile:filepath]; NSLog ( @ " %@ , data);
It is important to note that using plist to save data is only applicable to classes that are brought in from the foundation framework, such as Nsstring\nsarray\nsdictionary . When you save data, you write the file by calling the WriteToFile method of the object, while reading the data by calling the object's ... The Withcontentsoffile method reads the contents of the file.
Way two:
The essence of the preferences is actually to store the data in a plist way.
Preferences Save Data Code:
//Direct access to software preferences (library/preferences) with NsuserdefaultsNsuserdefaults *defaults =[Nsuserdefaults Standarduserdefaults]; [Defaults setobject:@"Test"Forkey:@" Account"]; [Defaults setobject:@"121"Forkey:@"Password"]; [Defaults Setinteger:TenForkey:@" Age"]; [Defaults setbool:yes forkey:@"Rember"]; //Sync now to sync memory information to a file[Defaults synchronize];
Preferences read Data code:
Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults]; *account = [Defaults objectforkey:@ "account"]; = [Defaults integerforkey:@ "Age"]; = [Defaults boolforkey:@ "rember"]; NSLog (@ "%@ %ld %d", Account,age,rember);
As you can see from the code, the difference between preferences and plist is that you save data using preferences, and you don't need to get directories and file names. In fact, when you use preferences to save data, you cannot create a new file and only save it to a fixed file.
In addition, because the nature of the preferences is still plist preservation, it is still possible to save only the classes that are in the foundation framework, or have some limitations.
Way three:
Archive the solution file.
This method can write any object that adheres to the Nscoding protocol to a file, and the data written to the file is garbled and has some encryption effect.
Implementation of the NSCODING protocol requires implementation of-(void) Encoderwithcoder: (Nscoder *) encoder and-(ID) Initwithcoder: (Nscoder *) decoder two methods.
Examples are as follows:
/* * * */-(void) Encodewithcoder when writing an object to a file: (Nscoder *) encoder{ [ Encoder encodeobject:_name Forkey:@ "name"]; [Encoder encodeinteger:_age Forkey: @" Age " ]; [Encoder encodedouble:_height Forkey: @" Height " ];}
/** * Called when an object is read from a file * In this method, it is clear how the property should be fetched*/- (ID) Initwithcoder: (Nscoder *) decoder{if(self =[Super Init]) {_name= [Decoder Decodeobjectforkey:@"name"]; _age= [Decoder Decodeintforkey:@" Age"]; _height= [Decoder Decodedoubleforkey:@"Height"]; } returnSelf ;}
The code that is archived for a model object:
persion *person = [[Persion alloc] init]; Person.name = @ " test " ; Person.age = 15 ; Person.height = 1.70 ; // Archive model objects NSString *doc = [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; NSString *filepath = [doc stringbyappendingpathcomponent:@ ' person.data
A method of extracting a model object:
NSString *doc = [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastObject]; *filepath = [doc stringbyappendingpathcomponent:@ "person.data"]; *person = [Nskeyedunarchiver Unarchiveobjectwithfile:filepath]; NSLog (@ "%@ %d %f", person.name,person.age,person.height);
Common ways for IOS app data storage