Analysis of archive and archive of OC objects and Analysis of oc archive files
Object archiving means to persistently store objects in the memory.
Object archive is to read the Persistent Object to the memory.
In oc, object archiving can be roughly divided into the following methods:
The quantity can be divided:
Objects can be divided into the following forms:
Next, write the code one by one for simple implementation.
1. Archive and archive a single system object:
Void SingleSystemObject () {NSArray * arr = [NSArray arrayWithObjects: @ 1, @ "hello", @ 3, nil]; NSString * filePath = [NSHomeDirectory () stringByAppendingPathComponent: @ "array.txt"]; // archive BOOL success = [NSKeyedArchiver archiveRootObject: arr toFile: filePath]; if (success) {NSLog (@ "");} // object archive NSArray * arr2 = [NSKeyedUnarchiver unarchiveObjectWithFile: filePath]; for (NSString * str in arr2) {NSLog (@ "% @", str );}}
2. Archive and archive Multiple System Objects
Void MultiSystemObject () {NSArray * arr = [NSArray arrayWithObjects: @ 1, @ 2, @ "", nil]; NSInteger integer = 10; BOOL archiverBool = YES; /* ------ object archive ---------------- */NSMutableData * data = [NSMutableData data]; NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] encoding: data]; // encode multiple objects for archiving, key is the identifier of the object when the file is Unarchived. You can write // encode with multiple overloading methods. However, not any system object has an overload method [archiver encodeObject: arr forKey: @ "arr"]; [archiver encodeInteger: integer forKey: @ "integer"]; [archiver encodeBool: archiverBool forKey: @ "archiverBool"]; // complete the encoding [archiver finishEncoding]; NSString * filepath = [NSHomeDirectory () stringByAppendingPathComponent: @ "1.txt"]; // write data to the file BOOL success = [data writeToFile: filepath atomically: YES] Through atomic operations; if (success) {NSLog (@ "");} /* ------ object archive ------------------ * // * ------ object archive -------------------- * // load the file data into the NSData object NSData * data2 = [NSData dataWithContentsOfFile: filepath]; // use the NSData object to initialize the NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data2]; // NSArray * arr2 = [unarchiver decodeObjectForKey: @ "arr"]; NSInteger integer2 = [unarchiver decodeIntegerForKey: @ "integer"]; BOOL unarchiveBool = [unarchiver decodeBoolForKey: @ "archiveBool"]; NSLog (@ "% @", arr2); NSLog (@ "% ld", integer2 ); NSLog (@ "% d", unarchiveBool);/* ------ object file --------------------*/}
3. Custom object archiving and archive
Custom object implementation can archive and archive, which must comply with the NSCoding protocol. There are two methods to implement under this Protocol:
Archive Encoding
-(Void) encodeWithCoder :( NSCoder *) ACO
Archive Decoding
-(Id) initWithCoder :( NSCoder *) aDecoder
The encoding and decoding methods for customizing the archive of member variables in custom objects are implemented in these two methods. Similar to the implementation method of multi-system object archiving, the member variables are encoded by type and key is formulated, during decoding, the key is decoded by type and assigned to the member variable. The object is finally returned. Simple code implementation is as follows:
Person. m
@interface Person : NSObject <NSCoding>@property (copy,nonatomic)NSString *name;@property (assign,nonatomic)NSInteger age;@end@implementation Person- (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeInteger:self.age forKey:@"age"];}- (id)initWithCoder:(NSCoder *)aDecoder{// self = [super init];// if (self) { self.name = [aDecoder decodeObjectForKey:@"name"]; self.age = [aDecoder decodeIntegerForKey:@"age"];// } return self;}- (NSString *)description{ NSString *desc = [NSString stringWithFormat:@"name = %@,age = %ld",self.name,self.age]; return desc;}@end
Archive archiving code:
Void UserDefinedObject () {Person * p = [[Person alloc] init]; p. name = @ "yangys"; p. age = 10; // archive NSString * filepath = [NSHomeDirectory () usage: @ "person.txt"]; BOOL success = [NSKeyedArchiver archiveRootObject: p toFile: filepath]; if (success) {NSLog (@ "" ");} // archive Person * person = [NSKeyedUnarchiver unarchiveObjectWithFile: filepath]; NSLog (@" % @ ", person );}
Customize the archiving and archive of multiple objects, such as archiving and archive of multiple system objects.
It is worth mentioning that, in the archiving and archive of system objects, only system objects that comply with the NSCoding Protocol or its subprotocols can be archived and archived.