ios如何把一个包含自定义对象的数组序列化到磁盘?
自定义对象实现NSCoding协议,在.m里面实现下面两个方法
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self) {
self.objectID = [aDecoder decodeObjectForKey:@'objectID'];
self.createdAt = [aDecoder decodeObjectForKey:@'createdAt'];
self.updatedAt = [aDecoder decodeObjectForKey:@'updatedAt'];
}
return self;
}-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.objectID forKey:@'objectID'];
[aCoder encodeObject:self.createdAt forKey:@'createdAt'];
[aCoder encodeObject:self.updatedAt forKey:@'updatedAt'];
}再然后用那个NSKeyedArchiver就可以了
赞0
踩0