plist : General Storage General objects: NSString, dictionaries, arrays, NSData
First, the first to set 2 buttons, the basic knowledge, it is not written in this, the direct Write button trigger event to store and read
-(Ibaction) Save
{
1. Get the Sandbox root path
NSString *home = Nshomedirectory ();
2.document Path
NSString *docpath = [Home stringbyappendingpathcomponent:@ "Documents"];
3. New data
Nsarray *data = @[@ "Jack", @10, @ "FFDSF"];
NSString *filepath = [DocPath stringbyappendingpathcomponent:@ "Data.plist"];
[Data Writetofile:filepath Atomically:yes];
}
Read button
-(ibaction) Read {
1. Get the Sandbox root path
NSString *home = Nshomedirectory ();
2.document Path
NSString *docpath = [Home stringbyappendingpathcomponent:@ "Documents"];
3. File path
NSString *filepath = [DocPath stringbyappendingpathcomponent:@ "Data.plist"];
4. Reading data
Nsarray *data = [Nsarray Arraywithcontentsoffile:filepath];
NSLog (@ "%@", data);
}
Nsuserdefault Storage
-(ibaction) Save {
1. Direct access to Software preferences (library/preferences) with Nsuserdefaults
Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];
2. Storing data
[Defaults setobject:@ "MJ" forkey:@ "account"];
[Defaults setobject:@ "123" forkey:@ "pwd"];
[Defaults setinteger:10 forkey:@ "age"];
[Defaults setbool:yes forkey:@ "Auto_login"];
3. Sync now
[Defaults synchronize];
}
-(ibaction) Read {
Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];
NSString *account = [Defaults objectforkey:@ "Account"];
BOOL autologin = [Defaults boolforkey:@ "Auto_login"];
NSLog (@ "%@-%d", account, Autologin);
}
Nskeyedarchiver Model method
Generic object that stores model objects, cannot store generic objects (dictionaries, strings, arrays, etc.)
Create a new 2 model
The model has the corresponding data
Mjstudent
#import <Foundation/Foundation.h>
@interface Mjstudent:nsobject <NSCoding>//compliance with nscoding Agreement
@property (nonatomic, copy) NSString *no;
@property (nonatomic, assign) double height;
@property (nonatomic, assign) int age;
In the. m file
@interface Mjstudent ()
@end
@implementation Mjstudent
/** Important
* When an object is written to a file, it is called
* In this method, clarify which attributes need to be stored
*/
If you do not write the encoding method, the system crashes directly
This is the encoding method of preserving the data.
-(void) Encodewithcoder: (Nscoder *) encoder
{
[Encoder encodeObject:self.no forkey:@ "no"];
[Encoder encodeInt:self.age forkey:@ "age"];
[Encoder encodeDouble:self.height forkey:@ "height"];
}
/** Important
* Called when parsing an object from a file
* In this method, clarify which attributes need to be stored
*/
This is the way to parse the data.
-(ID) Initwithcoder: (Nscoder *) decoder
{
if (self = [super init]) {
Read the contents of a file
self.no = [Decoder decodeobjectforkey:@ "no"];
Self.age = [Decoder decodeintforkey:@ "age"];
Self.height = [Decoder decodedoubleforkey:@ "height"];
}
return self;
}
Inside the Viewcontroller.
Save Data button
-(ibaction) Save {
1. New Model objects
Mjstudent *stu = [[Mjstudent alloc] init];
stu.no = @ "42343254";
Stu.age = 20;
Stu.height = 1.55;
2. Archiving Model objects
2.1. Get the full path of documents
NSString *doc = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObject];
2.2. Get the full path of the file
NSString *path = [Doc stringbyappendingpathcomponent:@ "Stu.data"];
2.3. Archive an Object
[Nskeyedarchiver Archiverootobject:stu Tofile:path];
}
Read Data button
-(ibaction) Read {
1. Get the full path of documents
NSString *doc = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObject];
2. Get the full path of the file
NSString *path = [Doc stringbyappendingpathcomponent:@ "Stu.data"];
3. Read the Mjstudent object from the file
Mjstudent *stu = [Nskeyedunarchiver Unarchiveobjectwithfile:path];
NSLog (@ "%@%d%f", stu.no, Stu.age, stu.height);
}
Three kinds of data storage (Plist,nsuserdefaults storage, Nskeyedarchiver model method)