該模型需要?dú)w檔解檔的屬性包含:
1,詞牌名/詩(shī)格的編號(hào)改化;2掩蛤,修改時(shí)間的數(shù)組;3陈肛,標(biāo)題揍鸟;4,內(nèi)容句旱;5阳藻,箋注;6谈撒,圖片數(shù)組腥泥;7,是否已存服務(wù)器啃匿;8道川,是否已完成。
準(zhǔn)備條件:需要對(duì)該模型的屬性進(jìn)行歸解檔立宜。遵循代理<NSCoding>
```
#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;
}
```
存儲(chǔ)分以下三步
第一步冒萄,設(shè)置Key值:注冊(cè)用戶(hù)ID+yyyyMMddhhmmss(注:該時(shí)間為創(chuàng)建時(shí)間)
第二步,把需要存儲(chǔ)的單個(gè)模型序列化成數(shù)據(jù)類(lèi)型橙数,并且存儲(chǔ)于偏好設(shè)置:
```
NSData*archiveCarPriceData = [NSKeyedArchiverarchivedDataWithRootObject:self.shiciModel];
[[NSUserDefaultsstandardUserDefaults]setObject:archiveCarPriceDataforKey:key];
```
第三步尊流,把key值寫(xiě)進(jìn)自己新建的Plist文件里作為數(shù)組保存:Exist.plist
```
//存儲(chǔ)Key值
NSMutableArray*arrayM = [[NSMutableArrayalloc]initWithContentsOfFile:BaseInfo.exitPath];
if(arrayM == nil) {
arrayM = [NSMutableArrayarray];
}
[arrayMaddObject:key];
[arrayMwriteToFile:BaseInfo.exitPathatomically:YES];
```
讀取分以下三步
1,讀取新建的plist文件獲取存儲(chǔ)的Key值數(shù)組:
```
- (NSArray*)existKeys {
return [NSArray arrayWithContentsOfFile:BaseInfo.exitPath];
}
```
2灯帮,在需要展示的時(shí)候遍歷讀取的數(shù)據(jù)Key值去偏好設(shè)置讀取模型數(shù)據(jù)崖技,通過(guò)反序列化轉(zhuǎn)化為模型:
```
/**
*讀取歸檔的內(nèi)容
*/
+ (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逻住,根據(jù)存儲(chǔ)的模型屬性,在需要的地方進(jìn)行有條件的展示迎献。