Object archiving refers to writing an object into a file and saving it on the hard disk. When the program is blocked again, these objects can be restored. Object archiving is also called object serialization or object persistence.
Persistent storage of data:
1. Object archiving nskeyedarchiver
2. nsuserdefaults
3. attribute list (nsarray, nsdictionary)
4. SQLite database and core data database
Archive format:
1. Archive objects in the foundation database. 2. Archive custom objects. to archive custom objects, the archive protocol nscoding is required.
1. The first archive method is implemented below to serialize and deserialize arrays.
Archive
// Serialize and deserialize an array nsstring * homedirectory = nshomedirectory (); // obtain the user's root directory nsarray * array = @ [@ 123, @ 456, @ "789", @ "88888"]; nsstring * drectory = [homedirectory stringbyappendingpathcomponent: @ "array. archive "]; bool issuccess = [nskeyedarchiver archiverootobject: array tofile: drectory]; If (issuccess) {nslog (@" serialized successfully ........ ");}
Archive
// Archive and deserialize nsarray * array1 = [nskeyedunarchiver unarchiveobjectwithfile: drectory]; nslog (@ "% @", array1 );
ArchiveDisadvantagesEach object must be archived with one file, and multiple objects must be archived,
Custom content archiving (this method is more convenient for archiving)
Archive
Use an nsdata instance as the archive storage data
Add archived content (set key and value)
Archive
Save archive data to disk
Archive
Reads files from a disk and generates an nsdata instance
Create and initialize an archive instance based on the data instance
Archive, access the value of value based on the key
// Archive nsstring * homedirectory = nshomedirectory (); nsstring * filepath = [homedirectory stringbyappendingpathcomponent: @ "custommsg. archive "]; nsmutabledata * Data = [nsmutabledata data]; nskeyedarchiver * archiver = [[nskeyedarchiver alloc] metadata: Data]; [archiver encodeint: 1000 forkey: @" num "]; [archiver encodeobject: @ "jimgreen" forkey: @ "name"]; [archiver finishencoding]; [archiver release]; [data writetofile: filepath atomically: Yes];
// Unarchive nsdata * mydata = [nsdata export: filepath]; nskeyedarchiver * unarchiver = [[initalloc] initforreadingwithdata: mydata]; int num = [unarchiver decodeintforkey: @ "num"]; nsstring * name = [unarchiver decodeobjectforkey: @ "name"]; nslog (@ "name ID % @, age is % d", name, num );
2. Custom object Archiving
To archive custom objects, You need to implement the nscoding protocol. The nscoding protocol has two methods: encodewithcoder to encode the object attribute data, and initwithcoder to decode the archive data to initialize the object.
After implementing the nscoding protocol, you can archive it with nskeyedarchiver.
The following is an example:
Archive the user class
. H file
#import <Foundation/Foundation.h>@interface User : NSObject<NSCoding>@property(nonatomic,copy)NSString *name;@property(nonatomic,copy)NSString *email;@property(nonatomic,copy)NSString *password;@property(nonatomic,assign)int age;@end
. M file
# Import "user. H "@ implementation user // encode attributes. During archiving, the-(void) encodewithcoder :( nscoder *) acder {[ACO encodeint: _ age forkey: @ "Age"]; [ecoder encodeobject: _ email forkey: @ "email"]; [ecoder encodeobject: _ name forkey: @ "name"]; [ecoder encodeobject: _ password forkey: @ "password"];} // decode the attribute. The-(ID) initwithcoder (nscoder *) is used for file decoding *) adecoder {If (Self = [Super init]) {_ age = [adecoder decodeintforkey: @ "Age"]; self. name = [adecoder decodeobjectforkey: @ "name"]; self. email = [adecoder decodeobjectforkey: @ "email"]; self. password = [adecoder decodeobjectforkey: @ "password"];} return self;} // this method is called when an object is printed in the % @ format-(nsstring *) description {nsstring * STR = [nsstring stringwithformat: @ "name is % @, age ID % d, email is % @, password ID % @", _ name, _ age, _ email, _ password]; return STR;}-(void) dealloc {[_ password release]; [_ email release]; [_ name release]; [Super dealloc];} @ end
Archive in main file
User * user = [[user alloc] init]; user. age = 24; user. name = @ "Jim Green"; user. email = @ "jim@qq.com"; user. password = @ "123456"; nsstring * Path = [nshomedirectory () stringbyappendingpathcomponent: @ "user. archive "]; bool issuccess = [nskeyedarchiver archiverootobject: User tofile: path]; If (issuccess) {nslog (@" success! "); [User release]; // archive the custom object}
Archive
User *user = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; NSLog(@"%@",user);