在oc中,我们可以对NSString,NSArray,NSDictionary,NSDate,NSNumber,对象进行编码解码操作,但是当我门要对自定义的类的对象进行编码解码时该怎么做呢,这时候就要用到NSCoding协议了,
NSCoding协议的方法
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder;
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"PersonName"];
// [aCoder encodeObject:self.age forKey:@"personAge"];
[aCoder encodeInt:self.age forKey:@"personAge"];
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
_name = [aDecoder decodeObjectForKey:@"PersonName"];
_age = [aDecoder decodeIntForKey:@"personAge"];
return self;
}