NSKeyedArchiver

Reprinted from: https://www.jianshu.com/p/2beb453fb9d3

1.1 What is Object Archive

Archiving is a very common method of file storage, and almost any type of object can be archived (actually a form of file storage).

Apple provides two classes, NSKeyedArchiver and NSKeyedUnarchiver, for us to serialize and deserialize objects, use NSKeyedArchiver for serialization before storing, and write to local files, and use NSKeyedUnarchiver for deserialization before use, to For extraction use!

1.2 In what scenarios will object archiving be used

If it is simple to carry out some system-provided types, such as NSArray, NSDictionary, NSString, BOOL, NSInteger, NSfloat and other basic data types or objects, we can choose the singleton NSUserDefault provided by the system, which is simple and convenient to use, but only for Are there any limitations for storing these specific data formats? And is the property property list safe in this way? Maybe none of these conditions NSUserDefault can meet!
For some regular, large-scale data, and data that can be followed regularly, we can choose to build a table or use the CoreData provided by Apple for persistent storage!

Then if the magnitude of the data is not very large, there is no need to use the database or CoreData, a weapon of mass destruction, and there are so many requirements for the security and durability of the data, we'd better choose Object serialization is a medium-kill tool!

1.3 How to use object archive 

We will not go into details about Apple's sandBox mechanism. The most important point is that the files stored in the Document directory in the sandBox will be synchronized to the apple server according to the user's appleID, that is to say, if the app is installed again , the files in the Document directory of the sandbox (sandBox) in this app will be restored again (the user's app purchase information is bound to the user's appleID), then the requirement is perfectly satisfied.

The most important point is that the current class needs to follow the NSCoding protocol. There are two methods in the NSCoding protocol, both of which are requred methods. After following this protocol, they must be implemented.

* Comply with NSCoping protocol

// called when archiving
- (void)encodeWithCoder:(NSCoder *)encoder {
    //Archive the parent class first
    [super encodeWithCode:encode];
    [encoder encodeObject:self.name forKey:@"name"];
    [encoder encodeInt:self.age forKey:@"age"];
    [encoder encodeFloat:self.height forKey:@"height"];
}
// Called when unarchiving
- (id)initWithCoder:(NSCoder *)decoder {
    //First unarchive the parent class
    self = [super initWithCoder:decoder];
    self.name = [decoder decodeObjectForKey:@"name"];
    self.age = [decoder decodeIntForKey:@"age"];
    self.height = [decoder decodeFloatForKey:@"height"];
    return self;
}

* Archive custom objects

//file(encode)
Person *person = [[Person alloc] init];
person.name = @"rh";
person.age = 100;
person.height = 1.83f;
[NSKeyedArchiver archiveRootObject:person toFile:path];





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325959523&siteId=291194637