Common ways for iOS app data storage
One, XML attribute list (plist) archive----Disadvantage: Cannot store custom objects
*plist can only store some of the regular classes that the system comes with, that is, objects with WriteToFile methods can use plist to save data, such as String/dictionary/array/nsnumber/nsdata ...,
* Because of the need to get the path, so more trouble
Ii. Preference (Preferences)----Disadvantages: Cannot store custom objects
* Preferences are designed to save configuration information for your application, and in general do not save other data in your preferences.
* If you use System Preferences to store data, the default is to store it under the Preferences folder. * Preferences will save all the data to the same file.
* Essentially plist (that is, you can't store normal objects), just encapsulate the operation that gets the path.
Third, Nskeyedarchiver archive (nscoding)
1. Customize a class Njperson, and declare the Name property, the Age property, the Height property
2. Drag 2 buttons on the storyboard, save the object and read the object, and listen for the 2 buttons (drag line)
3. Import the NJPerson.h in the controller implementation file and implement these 2 methods
-(Ibaction) Savebtnclick: (ID) sender{
1. Create an Object
Njperson *p = [[Njperson alloc] init];
P.name = @ "LNJ";
P.age = 28;
P.height = 1.76;
2. Get the file path
NSString *docpath = Nsserachpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes) lastObject];
NSString *path = [DocPath stringbyappendingpathcomponent:@ "Person.arc"];
3. Saving a custom object to a file
[Nskeyedarchiver archiverootobject:p Tofile:path];
}
-(Ibaction) Readbtnclick: (ID) sender{
1. Get the file path
NSString *docpath = Nsserachpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes) lastObject];
NSString *path = [DocPath stringbyappendingpathcomponent:@ "Person.arc"];
[Nskeyedarchiver archiverootobject:p Tofile:path];
2. Reading data
Njperson *p = [Nskeyedunarchiver Unarchiveobjectwithfile:path];
}
NSLog (@ "%@%d%.1f", p.name,p.age,p.height);
4. Compliance with the Nscoding protocol in NJPerson.h (Nscoding protocol must be implemented if you want to save a custom object to a file)
Implement Encodewithcoder in NJPERSON.M: Methods and Initwithcoder:
This method is called when you save a custom object to a file
Describes how to store the properties of a custom object in this method
That is, in this method, it is clear which properties of the custom object are stored
-(void) Encodewithcoder: (Nscoder *) encoder
{
[Encoder encodeObject:self.name forkey:@ "name"];
[Encoder encodeInteger:self.age forkey:@ "age"];
[Encoder encodeFloat:self.height forkey:@ "height"];
}
This method is called when an object is read from a file
Describes how to read objects saved in a file in this method
That is to say in this method how to read the object in the file
-(ID) Initwithcoder: (Nscoder *) decoder
{
if (self = [super init]) {
Self.name = [Decoder decodeobjectforkey:@ "name"];
self.age= [Decoder decodeobjectforkey:@ "age"];
self.height= [Decoder decodeobjectforkey:@ "height"];
}
return self;
}
Complete logic:
Click the Save Object button--Execute Savebtnclick: Method (Create object, get file path, save custom object to file)--[nscoding protocol] Execute Encoderwithcoder: Method
Click the Read Object button--Execute Readbtnclick: Method (get file path, read object from File)--[nscoding protocol] Execute Initwithcoder: Method
Inherited
1. Create a new class Njstudent, inherit from Njperson
2. Declare a weight attribute in its header file
3. Change to create Njstudent object, but to save and read the weight property, you must re-implement Encodewithcoder in the njstudent.m file: and Initwithcoder:, say how to save and how to read.
4. If you want to save both the subclass and the parent class, add [Super Encodewithcoder:acoder] to the Encodewithcoder: Method of the subclass.
5. If you want to read both the subclass and the parent class, then change the Initwithcoder: Method of the subclass to if (self = [super Initwithcoder:adecoder]).
Iv. SQLite3
Five, Core Data
iOS Development-data access