归档-对模型数组对象(存储到本地的plist文件)也数组里存放的是模型

一、模型文件

(1)JLMainViewsModel.h文件 必须遵循 NSCoding协议

@interface JLMainViewsModel : NSObject<NSCopying,NSCoding>

(2)JLMainViewsModel.m文件 实现代理方法

 @implementation JLMainViewsModel

 -(void)encodeWithCoder:(NSCoder *)aCoder{

     [aCoder encodeObject:self.fromSource forKey:@"fromSource"];
[aCoder encodeObject:self.url forKey:@"url"];
[aCoder encodeObject:self.title forKey:@"title"];
[aCoder encodeObject:self.type forKey:@"type"];
[aCoder encodeObject:self.newsTime forKey:@"newsTime"];
[aCoder encodeObject:self.imageUrl forKey:@"imageUrl"];
[aCoder encodeObject:self.imageArray forKey:@"imageArray"];
[aCoder encodeObject:self.bigImageUrl forKey:@"bigImageUrl"];
[aCoder encodeObject:self.bigImageArray forKey:@"bigImageArray"];
[aCoder encodeObject:self.recommend forKey:@"recommend"];
[aCoder encodeObject:self.exData forKey:@"exData"];
[aCoder encodeObject:self.newsType forKey:@"newsType"];
[aCoder encodeObject:self.style forKey:@"style"];
[aCoder encodeObject:self.gzh forKey:@"gzh"];
[aCoder encodeObject:self.uniqId forKey:@"uniqId"];
[aCoder encodeObject:self.subdesc forKey:@"subdesc"];
[aCoder encodeObject:self.autoplay forKey:@"autoplay"];
[aCoder encodeObject:self.fromicon forKey:@"fromicon"];
[aCoder encodeObject:self.webUrl forKey:@"webUrl"];
} -(id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
self.fromSource = [aDecoder decodeObjectForKey:@"fromSource"];
self.url = [aDecoder decodeObjectForKey:@"url"];
self.title = [aDecoder decodeObjectForKey:@"title"];
self.type = [aDecoder decodeObjectForKey:@"type"];
self.newsTime = [aDecoder decodeObjectForKey:@"newsTime"];
self.imageUrl = [aDecoder decodeObjectForKey:@"imageUrl"];
self.imageArray = [aDecoder decodeObjectForKey:@"imageArray"];
self.bigImageUrl = [aDecoder decodeObjectForKey:@"bigImageUrl"];
self.bigImageArray = [aDecoder decodeObjectForKey:@"bigImageArray"];
self.recommend = [aDecoder decodeObjectForKey:@"recommend"];
self.exData = [aDecoder decodeObjectForKey:@"exData"];
self.newsType = [aDecoder decodeObjectForKey:@"newsType"];
self.style = [aDecoder decodeObjectForKey:@"style"];
self.gzh = [aDecoder decodeObjectForKey:@"gzh"];
self.uniqId = [aDecoder decodeObjectForKey:@"uniqId"];
self.subdesc = [aDecoder decodeObjectForKey:@"subdesc"];
self.autoplay = [aDecoder decodeObjectForKey:@"autoplay"];
self.fromicon = [aDecoder decodeObjectForKey:@"fromicon"];
self.webUrl = [aDecoder decodeObjectForKey:@"webUrl"];
}
return self;
} - (id)copyWithZone:(NSZone *)zone
{
JLMainViewsModel *model = [[[self class] allocWithZone:zone]init];
return model;
}
@end

二、对模型数组进行存储 到本地

 //此段代码紧紧只是为了说明 存储段数组 是模型数组
NSMutableArray *mArray = [NSMutableArray array];
//字典转模型
for (NSDictionary *dict in array) {
JLMainViewsModel *news = [JLMainViewsModel newsWithDict:dict];
[mArray addObject:news];
}

这段代码才是 存储操作

 //1.获取存储的路径
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePatha = [documents stringByAppendingPathComponent:@"newsModelWithArray.plist"];
//归档
[NSKeyedArchiver archiveRootObject:mArray toFile:filePatha];

三、读取 归档的数据

 #pragma  mark - 获取本地缓存(归档)
-(BOOL)getNwsDataUnarchiver{
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *filePath = [documents stringByAppendingPathComponent:@"newsModelWithArray.plist"];
NSMutableArray<JLMainViewsModel *> *newsModelList = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
if (newsModelList.count > ) {
self.dataArray = newsModelList.mutableCopy;
return YES;
}
return NO;
}
上一篇:Android 使用SQLite存储以及读取Drawable对象


下一篇:JavaScript数组对象详情