Archive(归档)
需要归档的对象必须要遵循NSCoding协议,那么在调用archiverRootObject方法的时候,会自动调用该对象的encodeWithCoder方法
- (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:_name forKey:@"name"]; [aCoder encodeInteger:_age forKey:@"age"]; [aCoder encodeObject:_tel forKey:@"tel"]; [aCoder encodeObject:_icon forKey:@"icon"]; }
调用方法:[NSKeyedArchiver archiveRootObject:<#(id)#> toFile:<#(NSString *)#>];
Unarchive(恢复)
需要恢复的对象在调用unarchiveObjectWithFile方法的时候,会自动调用该对象的initWithCoder方法
- (id)initWithCoder:(NSCoder *)aDecoder{ NSLog(@"....."); _name = [aDecoder decodeObjectForKey:@"name"]; _age = [aDecoder decodeIntegerForKey:@"age"]; _tel = [aDecoder decodeObjectForKey:@"tel"]; _icon = [aDecoder decodeObjectForKey:@"icon"]; return self; }
调用方法:[NSKeyedUnarchiver unarchiveObjectWithFile:<#(NSString *)#> ];