How iOS data is storediOS development common data storage methods are: plist, Preferences nsuserdefaults, object archiving Nskeyedarchiver,SQLite3 and Core Data1. plist file Storageplist files are typically used to store user settings, using XML attribute lists to archive nsdictionary, Nsarray, NSNumber, and other types of datathe use of plist for data storage and reading, only for some of the system's own common types to useNote: plist cannot store custom objects 2. Preference Settings nsuserdefaultsPreferences are used to save settings such as user name, password, font size, auto login, and so on, primarily for storing app settings3. Archive NskeydearchiverBecause the first two have a fatal flaw, only the commonly used types can be stored. Archiving allows you to store your custom objects in a file. To use object archiving, an object must implement the Nscoding protocol. Most object C objects conform to the Nscoding protocol, or you can implement the Nscoding protocol in a custom object to implement the Nscoding protocol4. SQLite3SQLite is a lightweight embedded database, both Android and iOS development using SQLite database in project development, usually need to do offline caching of data processing, such as the offline caching of news data. Offline caching typically saves data to the project's sandbox, butThe three methods above have a fatal disadvantage, that is, can not store large quantities of data, there is a performance problem. If the use of archiving, two problems: (1) data access must be complete, require write time to write once, read the time to read all at once, which involves the application of performance issues. (2) If there are 1000 data, at this time to deposit the 1001th piece of data, then need to take all the data out, add this data, and then deposit. Note: The above three technologies cannot handle the storage of large quantities of data, and large quantities of data are usually stored using a database. 5. Core Data core data is a package for SQLite because the SQLite used in iOS is a pure C language. 1. How to store/read plist files
//file pathNSString *path = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) LastObject] stringByAppendingPathComponent:@"data.plist"];//Setting up dataNsarray *data = @[@"Jack", @Ten,@"ABCD"];//Storage plist[Data Writetofile:path atomically:yes];//ReadNsarray *data2 = [Nsarray Arraywithcontentsoffile:path];
2. Preference Settings Nsuserdefaults
/*Preferences--there are properties in the data storage class*/- (void) Save {//1. Direct access to Software preferences (library/preferences) with NsuserdefaultsNsuserdefaults *defaults =[Nsuserdefaults standarduserdefaults];//2. Storing Data[Defaults setobject:@"qr"Forkey:@"name"]; [Defaults setobject:@"123"Forkey:@"pwd"]; [Defaults Setinteger:TenForkey:@" Age"]; [Defaults setbool:yes forkey:@"Auto_login"];//3. Sync now[Defaults synchronize]; }
/* Preferences-read Data */-(void) Read { *defaults = [nsuserdefaults Standarduserdefaults]; *account = [Defaults objectforkey:@ "account"]; = [Defaults boolforkey:@ "auto_login"]; NSLog (@ "%@-%d", Account, autologin);}
3. Object Archive Nskeyedarchiver
/*Archive--Store custom Objects*/ Person*p =[[Person alloc] init];p.= -; P.name=@"qr"; //Stitching file nameNSString *path = Nstemporarydirectory () stringbyappendingpathcomponent:@"Person.data"]; [Nskeyedarchiver archiverootobject:p Tofile:path];//solution filePerson *p = [Nskeyedunarchiver Unarchiveobjectwithfile:filepath];
/*the method to be implemented in the person class*///Compliance < nscoding > agreements//When to call: This method is called when an object is to be archived.//role: Tell Apple which attributes in the current object need to be archived- (void) Encodewithcoder: (Nscoder *) acoder{[Acoder encodeobject:_name forkey:@"name"]; [Acoder encodeint:_age Forkey:@" Age"];} ————————————————————————————//action: Tell Apple which attributes in the current object need to be unpacked//Initwithcoder when to invoke: Whenever a file is parsed, it is called- (ID) Initwithcoder: (Nscoder *) adecoder{#warning[Super Initwithcoder]//You cannot use [super Initwithcoder] and when to invoke [Super Initwithcoder:adecoder]: As long as the parent class complies with the Nscoding protocol, it calls the if(self =[Super Init]) { //solution file//Be sure to remember to assign values to member properties_name = [Adecoder decodeobjectforkey:@"name"]; _age= [Adecoder Decodeintforkey:@" Age"]; } returnSelf ;}
/* Follow-up given */
4. SQLite3
5. Core Data
iOS data storage--2 Storage method