一、模型文件
(1)JLMainViewsModel.h文件 必须遵循 NSCoding协议
1 @interface JLMainViewsModel : NSObject<NSCopying,NSCoding>
(2)JLMainViewsModel.m文件 实现代理方法
1 @implementation JLMainViewsModel 2 3 -(void)encodeWithCoder:(NSCoder *)aCoder{ 4 5 [aCoder encodeObject:self.fromSource forKey:@"fromSource"]; 6 [aCoder encodeObject:self.url forKey:@"url"]; 7 [aCoder encodeObject:self.title forKey:@"title"]; 8 [aCoder encodeObject:self.type forKey:@"type"]; 9 [aCoder encodeObject:self.newsTime forKey:@"newsTime"]; 10 [aCoder encodeObject:self.imageUrl forKey:@"imageUrl"]; 11 [aCoder encodeObject:self.imageArray forKey:@"imageArray"]; 12 [aCoder encodeObject:self.bigImageUrl forKey:@"bigImageUrl"]; 13 [aCoder encodeObject:self.bigImageArray forKey:@"bigImageArray"]; 14 [aCoder encodeObject:self.recommend forKey:@"recommend"]; 15 [aCoder encodeObject:self.exData forKey:@"exData"]; 16 [aCoder encodeObject:self.newsType forKey:@"newsType"]; 17 [aCoder encodeObject:self.style forKey:@"style"]; 18 [aCoder encodeObject:self.gzh forKey:@"gzh"]; 19 [aCoder encodeObject:self.uniqId forKey:@"uniqId"]; 20 [aCoder encodeObject:self.subdesc forKey:@"subdesc"]; 21 [aCoder encodeObject:self.autoplay forKey:@"autoplay"]; 22 [aCoder encodeObject:self.fromicon forKey:@"fromicon"]; 23 [aCoder encodeObject:self.webUrl forKey:@"webUrl"]; 24 } 25 26 -(id)initWithCoder:(NSCoder *)aDecoder{ 27 if (self = [super init]) { 28 self.fromSource = [aDecoder decodeObjectForKey:@"fromSource"]; 29 self.url = [aDecoder decodeObjectForKey:@"url"]; 30 self.title = [aDecoder decodeObjectForKey:@"title"]; 31 self.type = [aDecoder decodeObjectForKey:@"type"]; 32 self.newsTime = [aDecoder decodeObjectForKey:@"newsTime"]; 33 self.imageUrl = [aDecoder decodeObjectForKey:@"imageUrl"]; 34 self.imageArray = [aDecoder decodeObjectForKey:@"imageArray"]; 35 self.bigImageUrl = [aDecoder decodeObjectForKey:@"bigImageUrl"]; 36 self.bigImageArray = [aDecoder decodeObjectForKey:@"bigImageArray"]; 37 self.recommend = [aDecoder decodeObjectForKey:@"recommend"]; 38 self.exData = [aDecoder decodeObjectForKey:@"exData"]; 39 self.newsType = [aDecoder decodeObjectForKey:@"newsType"]; 40 self.style = [aDecoder decodeObjectForKey:@"style"]; 41 self.gzh = [aDecoder decodeObjectForKey:@"gzh"]; 42 self.uniqId = [aDecoder decodeObjectForKey:@"uniqId"]; 43 self.subdesc = [aDecoder decodeObjectForKey:@"subdesc"]; 44 self.autoplay = [aDecoder decodeObjectForKey:@"autoplay"]; 45 self.fromicon = [aDecoder decodeObjectForKey:@"fromicon"]; 46 self.webUrl = [aDecoder decodeObjectForKey:@"webUrl"]; 47 } 48 return self; 49 } 50 51 - (id)copyWithZone:(NSZone *)zone 52 { 53 JLMainViewsModel *model = [[[self class] allocWithZone:zone]init]; 54 return model; 55 } 56 @end
二、对模型数组进行存储 到本地
1 //此段代码紧紧只是为了说明 存储段数组 是模型数组 2 NSMutableArray *mArray = [NSMutableArray array]; 3 //字典转模型 4 for (NSDictionary *dict in array) { 5 JLMainViewsModel *news = [JLMainViewsModel newsWithDict:dict]; 6 [mArray addObject:news]; 7 }
这段代码才是 存储操作
1 //1.获取存储的路径 2 NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 3 NSString *filePatha = [documents stringByAppendingPathComponent:@"newsModelWithArray.plist"]; 4 //归档 5 [NSKeyedArchiver archiveRootObject:mArray toFile:filePatha];
三、读取 归档的数据
1 #pragma mark - 获取本地缓存(归档) 2 -(BOOL)getNwsDataUnarchiver{ 3 NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]; 4 NSString *filePath = [documents stringByAppendingPathComponent:@"newsModelWithArray.plist"]; 5 NSMutableArray<JLMainViewsModel *> *newsModelList = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; 6 if (newsModelList.count > 0) { 7 self.dataArray = newsModelList.mutableCopy; 8 return YES; 9 } 10 return NO; 11 }