该模型需要归档解档的属性包含:
1,词牌名/诗格的编号;2,修改时间的数组;3,标题;4,内容;5,笺注;6,图片数组;7,是否已存服务器;8,是否已完成。
准备条件:需要对该模型的属性进行归解档。遵循代理
```
#pragma mark - NSCoding 的代理方法
// 归档
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.titleText forKey:@"titleText"];
[aCoder encodeObject:self.annotation forKey:@"annotation"];
[aCoder encodeObject:self.poetryType forKey:@"poetryType"];
[aCoder encodeObject:self.alter_time forKey:@"alter_time"];
[aCoder encodeObject:self.imageArray forKey:@"imageArray"];
[aCoder encodeObject:self.contentText forKey:@"contentText"];
[aCoder encodeObject:self.isCompliment forKey:@"isCompliment"];
[aCoder encodeObject:self.isExistServer forKey:@"isExistServer"];
}
// 解档
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self == [super init]) {
self.titleText = [aDecoder decodeObjectForKey:@"titleText"];
self.poetryType = [aDecoder decodeObjectForKey:@"poetryType"];
self.alter_time = [aDecoder decodeObjectForKey:@"alter_time"];
self.annotation = [aDecoder decodeObjectForKey:@"annotation"];
self.imageArray = [aDecoder decodeObjectForKey:@"imageArray"];
self.contentText = [aDecoder decodeObjectForKey:@"contentText"];
self.isCompliment = [aDecoder decodeObjectForKey:@"isCompliment"];
self.isExistServer = [aDecoder decodeObjectForKey:@"isExistServer"];
}
return self;
}
```
存储分以下三步
第一步,设置Key值:注册用户ID+yyyyMMddhhmmss(注:该时间为创建时间)
第二步,把需要存储的单个模型序列化成数据类型,并且存储于偏好设置:
```
NSData*archiveCarPriceData = [NSKeyedArchiverarchivedDataWithRootObject:self.shiciModel];
[[NSUserDefaultsstandardUserDefaults]setObject:archiveCarPriceDataforKey:key];
```
第三步,把key值写进自己新建的Plist文件里作为数组保存:Exist.plist
```
//存储Key值
NSMutableArray*arrayM = [[NSMutableArrayalloc]initWithContentsOfFile:BaseInfo.exitPath];
if(arrayM == nil) {
arrayM = [NSMutableArrayarray];
}
[arrayMaddObject:key];
[arrayMwriteToFile:BaseInfo.exitPathatomically:YES];
```
读取分以下三步
1,读取新建的plist文件获取存储的Key值数组:
```
- (NSArray*)existKeys {
return [NSArray arrayWithContentsOfFile:BaseInfo.exitPath];
}
```
2,在需要展示的时候遍历读取的数据Key值去偏好设置读取模型数据,通过反序列化转化为模型:
```
/**
*读取归档的内容
*/
+ (NSArray *)getCoderModel{
NSMutableArray*arrayM = [NSMutableArrayarrayWithCapacity:BaseInfo.existKeys.count];
[BaseInfo.existKeysenumerateObjectsUsingBlock:^(NSString*_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {
NSData*myEncodedObject = [[NSUserDefaultsstandardUserDefaults]objectForKey:obj];
SCIShiciModel*model = [[SCIShiciModelalloc]init];
model = [NSKeyedUnarchiverunarchiveObjectWithData:myEncodedObject];
[arrayMaddObject:model];
}];
return[arrayMmutableCopy];
}
```
3,根据存储的模型属性,在需要的地方进行有条件的展示。