Archiving objects is similar to copying objects, that is, the Protocol is required. Why is there no archiving protocol for nsstring and nsnumber objects in the past? Because they come with their own.
Therefore, if we want to archive custom objects (instead of the default objects in Foundation), we need to introduce the archive protocol <nscoding>.
For example, declare a user class, create an object user1, archive this user1, and archive the archived file to user2 to see if it is the original data.
(1) user. h
# Import <Foundation/Foundation. h> @ interface User: nsobject <nscoding> // to archive and archive custom objects, You need to introduce the protocol, just like copying objects, this class will introduce the same copy protocol as @ property (nonatomic, copy) nsstring * Name; @ property (nonatomic, copy) nsstring * email; @ property (nonatomic, copy) nsstring * passwd; @ property (nonatomic, assign) int age; @ end
(2) user. m
# Import "user. H "@ implementation user // The following two function formats are default. You can click nscoding to view and copy them.-(void) encodewithcoder :( nscoder *) ACO {[ACO encodeint: _ age forkey: @ "Age"]; [ecoder encodeobject: _ name forkey: @ "name"]; [ecoder encodeobject: _ email forkey: @ "email"]; [ACO der encodeobject: _ passwd forkey: @ "passwd"];}-(ID) initwithcoder :( nscoder *) adecoder {self = [Super init]; If (self! = Nil) {_ age = [adecoder decodeintforkey: @ "Age"]; self. name = [adecoder decodeobjectforkey: @ "name"]; self. email = [adecoder decodeobjectforkey: @ "email"]; self. passwd = [adecoder decodeobjectforkey: @ "passwd"];} return self ;}@ end
(3) Main. m
# Import <Foundation/Foundation. h> # import "user. H "int main (INT argc, const char * argv []) {@ autoreleasepool {// assign a value to user * user1 = [user alloc] init] When initializing an object disease. user1.age = 20; [email protected] "Jack"; [email protected] "[email protected]"; [email protected] "123456 "; // create the file path nsstring * filepath = [nshomedirectory () stringbyappendingpathcomponent: @ "self. arc "]; // archive this object bool success = [nskeyedarchiver archiverootobject: user1 tofile: filepath]; If (SUCCESS) {nslog (@" success ");} // unarchive user * user2 = [nskeyedunarchiver unarchiveobjectwithfile: filepath]; // unarchive nslog to another object (@ "% d, % @", user2.age, user2.name, user2.email, user2.passwd); // output, intact} return 0 ;}
Archive results to generate a file:
Archiving result:
20,jack,[email protected],123456
Combine the previous notes to summarize:
A: When archiving, there is an object first, then there is a final file path, and then using [nskeyedarchiver archiverootobject: <# (ID) #> tofile: <# (nsstring *) #>] archive the object to this file;
B: The Archive object is the type of the archive object. You can use the archive object to obtain the archive object, this object can be used normally after the archive is Unarchived;
C: There are two ways to archive objects: Simply put, the object is directly archived into a file. Another complex example is the previous example, archive data objects> write data to data using the archive function> write data to the final file, the archive path is to first connect the file to a data object and then call the data in it. In fact, data is generally in a dictionary format;
D: according to whether the object is self-built or custom, there are some differences in archiving: to archive a custom object, the archive protocol must be introduced to its class <nscoding>;
E: the archived data is encrypted.
[OC learning-28] archiving and archiving of custom objects: Example and summary