一、nsuserdefault:
1:nsuserDefault 不能存储自定义对象, 其支持的数据格式有:NSNumber(Integer、Float、Double等)、NSString、NSDate、NSArray(成员必须也是支持的格式类型)、NSDictionary(同NSArray)。
二、归档:
1.归档保存的自定对象的重写归档方法时,需要将基本数据类型转成nsnumber对象。
2.归档需要重写两个方法:
//序列化
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:@(self.cellType) forKey:CELLTYPE];
[coder encodeObject:self.cellContent forKey:CELLCONTENT];
[coder encodeObject:@(self.cellHeight) forKey:CELLHEIGHT];
[coder encodeObject:self.cellImage forKey:CELLIMAGE];
[coder encodeObject:@(self.isHiddenPlayButton) forKey:ISHIDDENPLAYBUTTON];
[coder encodeObject:self.cellImageUrl forKey:CELLIMAGEURL];
[coder encodeObject:self.cellImageData forKey:CELLIMAGEDATA];
[coder encodeObject:self.cellVideoUrl forKey:CELLVIDEOURL];
}
//反序列化
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super init];
if (self) {
self.cellType = (CellType)[[coder decodeObjectForKey:CELLTYPE] integerValue];
self.cellContent = [coder decodeObjectForKey:CELLCONTENT];
self.cellHeight = [[coder decodeObjectForKey:CELLHEIGHT] floatValue];
self.cellImage = [coder decodeObjectForKey:CELLIMAGE];
self.isHiddenPlayButton = [[coder decodeObjectForKey:ISHIDDENPLAYBUTTON] boolValue];
self.cellImageUrl = [coder decodeObjectForKey:CELLIMAGEURL];
self.cellImageData = [coder decodeObjectForKey:CELLIMAGEDATA];
self.cellVideoUrl = [coder decodeObjectForKey:CELLVIDEOURL];
}
return self;
}
3.归档只能每次保存一个自定义对象。