A simple explanation
In the use of plist for data storage and reading, only applicable to some of the system's own common types to use, and must first get the path relative trouble; preferences (keep everything under the same folder and primarily store app settings information) Archive: Because 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.
Second, code example
1. File structure
2. Code examples
YYVIEWCONTROLLER.M file
1//2//YYVIEWCONTROLLER.M 3//02-Archive 4//5//Created by Apple on 14-6-7. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" #import "YYPerson.h" one @interface Yyviewcontroller ()-(ibaction) Savebtnonc Lick: (ID) sender;14-(Ibaction) Readbtnonclick: (ID) sender;15 @end17 @implementation YYViewController19-(void) viewDidLoad21 {[Super viewdidload];23}24-(ibaction) Savebtnonclick: (ID) Sender {27//1. Create Object Yypers On *p=[[yyperson alloc]init];29 [email protected] "Top of text"; p.age=23;31 p.height=1.7;32 33//2. Get file Path NSString *docpath=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) LastObject] ; NSString *path=[docpath stringbyappendingpathcomponent:@ "Person.yangyang"];36 NSLog (@ "path=%@", Path); 37 38 3. Saving a custom object to a file [nskeyedarchiver archiverootobject:p tofile:path];40}42-(ibaction) Readbtnonclick :(ID) Sender {44 1. Get File path NSString *docpath=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) las tobject];46 nsstring *path=[docpath stringbyappendingpathcomponent:@ "Person.yangyang"];47 NSLog (@ "path=%@", path); 4 8 49//2. Read object from File Yyperson *p=[nskeyedunarchiver unarchiveobjectwithfile:path];51 NSLog (@ "%@,%d,%.1f", p. name,p.age,p.height);}53 @end
Create a new Person class
YYPerson.h file
1//2// YYPerson.h 3// 02-Archive 4//5// Created by Apple on 14-6-7.6// Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import <foundation/foundation.h>10 11//If you want to save a custom object to a file, you must implement the Nscoding protocol @interface YYPERSON:NSOB Ject<nscoding>13 14//Name @property (nonatomic,copy) NSString *name;16//Age @property (nonatomic,assign) int. ; 18//Height @property (nonatomic,assign) Double height;20 @end
YYPERSON.M file
1//2//YYPERSON.M 3//02-Archive 4//5//Created by Apple on 14-6-7.6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYPerson.h" @implementation YYPerson12 13//When a custom object is saved to a file, the method 14//is described in this method to store the properties of the custom object 15/ /also say in this method that it is clear which properties of the custom object are stored (void) Encodewithcoder: (Nscoder *) aCoder17 {NSLog (@ "called Encodewithcoder: Method"); [ACo] Der encodeObject:self.name forkey:@ "name"];20 [Acoder encodeInteger:self.age forkey:@ "Age"];21 [Acoder Encodedoubl E:self.height forkey:@ "Height"];22}23 24//When an object is read from a file, the method 25//is described in this method to read the object stored in the file 26// That is, in this method it is clear how to read the object in the file (ID) Initwithcoder: (Nscoder *) aDecoder28 {NSLog (@ "called Initwithcoder: Method"); 30//Note: In the construction method method to initialize parent class first (Self=[super init]) {self.name=[adecoder decodeobjectforkey:@ "name"];33 self.age=[ Adecoder decodeintegerforkey:@ "Age"];34 self.height=[adecoder decodedoubleforkey:@ "height"];35}36 return self;37}38 @end
3. Printing effects and two important error hints
Click the Save button and the Read button to successfully print the results as follows:
For an error message that does not implement two protocol methods:
-(void) Encodewithcoder: (Nscoder *) Acoder method:
-(ID) Initwithcoder: (Nscoder *) Adecoder method:
Third, the use of the inheritance class
Create a new student class, and let this class inherit from the Preson class, adding a weight attribute.
YYstudent.h file
1//2// YYstudent.h 3// 02-Archive 4//5// Created by Apple on 14-6-7.6// Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYPerson.h" @interface yystudent:yyperson12//Add a weight property of @property (Nonatomic,assign) Double W Eight;14 @end
YYSTUDENT.M file
1//2// YYSTUDENT.M 3// 02-Archive 4//5// Created by Apple on 14-6-7.6// Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYstudent.h" @implementation YYstudent12 13//Override both methods in subclasses-(void) Encodewithcoder: (Nscoder *) aCoder15 {" super encodewithcoder:acoder];17 NSLog (@" called Yystudent Encodewithcoder "); [Acoder EncodeFloat:self.weight forkey:@ "Weight"];19}20-(ID) Initwithcoder: (Nscoder *) ADecoder22 { if (self = [Super I Nitwithcoder:adecoder]) { NSLog (@ "called Yystudent Initwithcoder"); self.weight = [Adecoder decodefloatforkey:@ "Weight"];26 }27 return self;28}29 @end
YYVIEWCONTROLLER.M file
1//2//YYVIEWCONTROLLER.M 3//02-Archive 4//5//Created by Apple on 14-6-7. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" #import "YYPerson.h" #import "YYstudent.h" @interface Yyviewcontroller () 1 4-(Ibaction) Savebtnonclick: (ID) sender;15-(Ibaction) Readbtnonclick: (ID) sender;16 @end18 @implementation YYViewController20-(void) viewDidLoad22 {[Super viewdidload];24}25-(ibaction) Savebtnonclick: (ID) sender {28//1. Create Object//Yyperson *p=[[yyperson alloc]init];30//[email protected] "Top of text";//P.AGE=23;32// p.height=1.7;33 yystudent *s=[[yystudent alloc]init];35 [email protected] "wendingding"; s.age =23;37 s.height=1.7;38 s.weight=62;39//2. Get file path all NSString *docpath=[nssearchpathfordirectoriesindomains ( NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];41 nsstring *path=[docpath stringbyappendingpathcomponent:@ " Person.yangyang "];42 NSLog (@ "path=%@", Path); 43 44//3. Saving a custom object to a file//[Nskeyedarchiver archiverootobject:p tofile:path];46 [Nskeyedarchiver archiverootobject:s tofile:path];47}49-(ibaction) Readbtnonclick: (ID) Sender {51//1. Get File path NSString *docpath=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastobject];53 nsstring *path=[docpath stringbyappendingpathcomponent:@ "Person.yangyang"];54 NSLog (@ "path=%@", Path ); 55 56//2. Read object from File//Yyperson *p=[nskeyedunarchiver unarchiveobjectwithfile:path];58//NSLog (@ "%@,%d,% .1f ", p.name,p.age,p.height); Yystudent *s=[nskeyedunarchiver unarchiveobjectwithfile:path];60 NSLog (@"%@,%d,%.1f ,%f ", s.name,s.age,s.height,s.weight);}62 @end
Click the Save button and the print output after reading the button:
Iv. Important Notes
1. Save the data process:
1. Create Object yystudent *s=[[yystudent alloc]init]; [Email protected] "wendingding"; s.age=23; s.height=1.7; s.weight=62; 2. Get file path nsstring *docpath=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) Lastobject]; NSString *path=[docpath stringbyappendingpathcomponent:@ "Person.yangyang"]; NSLog (@ "path=%@", path); 3. Save the custom object to a file [Nskeyedarchiver archiverootobject:s Tofile:path];
2. Read the data process:
1. Get file path nsstring *docpath=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) Lastobject]; NSString *path=[docpath stringbyappendingpathcomponent:@ "Person.yangyang"]; 2. Reading objects from a file yystudent *s=[nskeyedunarchiver Unarchiveobjectwithfile:path];
3. Comply with the Nscoding agreement and implement the two methods in the agreement.
4. In the case of inheritance, the subclass must override the two methods. Because the child of the person in the access time, will go to the subclass to find the method of invocation, not found then it went to the parent class, so the last time to save and read the new properties will be ignored. You need to call the parent class's method first, initialize the parent class, and then initialize the subclass.
5. The suffix name of the file that holds the data can be named arbitrarily.
6. Data saved through Plist is displayed directly and is not secure. The data saved by the archive method is garbled and more secure when it is opened in a file.
iOS Development UI Chapter-ios Application Data Storage (archive)