一、模型文件

(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;
}

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

  1. 什么是“类数组对象”,在jquer中怎样将类数组对象转换为数组对象

    类数组对象的定义: 所谓"类数组对象"就是一个常规的Object对象,如$("div")但它和数组对象非常相似:具备length属性, 并以0.1.2.3……等 ...

  2. 解析plist文件(字典里包着数组,数组中又包含字典)

    #import "RootTableViewController.h" #import "City.h" @interface RootTableViewCon ...

  3. iOS自己定义对象保存到本地文件

    我是将聊天记录存到本地,里边用到了自己定义的对象.把数据转成Data格式存到本地.在转Data格式的时候报错了.这时候须要先将自己定义对象进行归档才干够转Data格式. 方法例如以下: 一.在.h文件 ...

  4. 【系统设计】S3 对象存储

    在本文中,我们设计了一个类似于 Amazon Simple Storage Service (S3) 的对象存储服务.S3 是 Amazon Web Services (AWS) 提供的一项服务, 它 ...

  5. 阿里云对象存储 OSS 应用服务器搭建代码

    背景说明 最近做一个APP客户端图片直传阿里云OSS的服务,需要在后台开一个阿里云的OSSToken获取的接口. 阿里云官方文档地址:快速搭建移动应用直传服务. 略过移动端说明,直接看服务端的. 不是 ...

  6. JavaScript -基础- 函数与对象(三)数组对象

    一.数组对象 1.创建方式 1)创建方式一 var arr=[1,2,3]; 2)创建方式二 var arr2=new Array(1,2,3); 注意: 数组中可以存储任何数据类型.方法类型(Jav ...

  7. 数组对象(NSArray和NSMutableArrray)

    Objective-C中除了可以使用C中的基本数组外,如int[5],char word[] ={‘a’,'b’,'c’};Foundation还提供了NSArray类.Foundation是有序的对 ...

  8. NodeList类数组对象: HTMLCollection , NamedNodeMap,两套API(childNodes , children)

    快捷键:leishuzuduixiang(类数组对象)  bianlijiedian(遍历节点)  jiedian(节点)  htmlcollection , namednodemap , nodel ...

  9. QingStor 对象存储架构设计及最佳实践

    对象存储概念及特性 在介绍 QingStor️对象存储内部的的架构和设计原理之前,我们首先来了解一下对象存储的概念,也就是从外部视角看,对象存储有什么特性,我们应该如何使用. 对象存储本质上是一款存储 ...

随机推荐

  1. CentOS-7-64bit 下为firefox安装flashplayer

    最近更新了Centos 7 还是有一些不习惯的 给ff安flashplayer插件时,按centos 6.x的方法时都无法成功,后来find了一下,才知道firefox还有一个64bit的文件夹: 解 ...

  2. 【bzoj2286】[Sdoi2011]消耗战

    虚树入门题: #include<cstdio> #include<cstring> #include<algorithm> #include<ctime> ...

  3. Ubuntu下安装Python3.4及用python编译py文件

    1.安装python 3.4程序 sudo apt-get install python3.4 2.python 3.4是被默认安装在/usr/local/lib/python3.4,删除默认pyth ...

  4. sdut oj 3058 路线冲突问题(BFS+记录路径算法,回溯路径 )

    路线冲突问题 题目描述 给出一张地图,地图上有n个点,任意两点之间有且仅有一条路.点的编号从1到n. 现在兵团A要从s1到e1,兵团B要从s2到e2,问两条路线是否会有交点,若有则输出交点个数,否出输 ...

  5. finalize方法

    什么是垃圾回收机制 不定时去堆内存中清理不可达对象.不可达的对象并不会马上就会直接回收, 垃圾收集器在一个Java程序中的执行是自动的,不能强制执行,即使程序员能明确地判断出有一块内存已经无用了,是应 ...

  6. android:Android中用文件初始化sqlite数据库

    很多时候在应用安装初始化时,需要创建本地数据库,同时为数据库添加数据,之后再从数据库中读取数据. 这里有2个思路 1.先在本地创建一个能支持android使用的sqlite数据库文件,启动时,用现成的 ...

  7. php排序方法之快速排序

    $arr = array(3,55,45,2,67,76,6.7,-65,85,4); function quickSort($arr){ if (count($arr) <= 1){ retu ...

  8. 渲染树render tree

    CSSOM树和DOM树连接在一起形成一个render tree,渲染树用来计算可见元素的布局并且作为将像素渲染到屏幕上的过程的输入. DOM树和CSSOM树连接在一起形成render tree . r ...

  9. H3C-交换机端口绑定

    1.端口和MAC地址绑定: (1)使用am命令: [switch]am user-bind mac-address 00e0-fc23-f8d3 interface Ehternet 0/1 (2)使 ...

  10. C语言教学杂记——字母排序

    一个人在被告诉一个问题应该怎么被解决后,而且亲身试验效果OK后,一旦遇到类似的问题,就会条件反射般直接拿这个方法来用了.很少会去想为什么要用这个方法,会不会有什么隐患,还有没有别的方法呢,等等这些问题 ...