OC学习 ----- 字典转模型
来源:互联网 发布:app软件定制开发 编辑:程序博客网 时间:2024/06/05 04:49
模型类 , 即用来存放数据的类, 继承自NSObject.
} // 赋值 _questions = questionArray; } return _questions; }
所谓模型,其实就是数据模型, 专门用来存放数据的对象,用它来表示会更加专业.
模型设置数据和取出数据都是通过它的属性, 属性名如果写错,编译器会马上报错,因此保证了数据的正确性.
使用模型访问属性时, 编译器会提供一系列的提示,提高编码效率.
字典转模型应该提供一个可以传入字典参数的构造方法. 一般提供如下两个方法(一个对象方法和一个类方法) (为了与苹果公司原代码保持一致性,方法名称尽量模仿苹果公司规范来书写)
<span style="font-size:14px;">-(instancetype)initWithDict:(NSDictionary *)dict;+(instancetype)XXXWithDict:(NSDictionary *)dict;</span>
instancetype:在类型表示上,跟id一样,可以表示任何数据类型;
instancetype只能用在返回值类型上,不能像id一样用在参数类型上;
instancetype比id多一个好处:编译器会检测instancetype的真实类型.
根据数据字典的键值创建对应的数据模型. 在.h文件中声明模型属性及方法 .如下
<span style="font-size:14px;">#import <Foundation/Foundation.h>@interface App : NSObject/** * 名称 */@property (nonatomic, copy) NSString *name;/** * 图标 */@property (nonatomic, copy) NSString *icon;/** * 标题 */@property (nonatomic, weak) NSString *title;/** * 答案 */@property (nonatomic, copy) NSString *answer; /** * 通过字典来初始化模型对象 * * @param dict 字典对象 * * @return 已经初始化完毕的模型对象 */- (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)<strong>app</strong>WithDict:(NSDictionary *)dict;@end</span>字典转模型实现方法如下:
<span style="font-size:14px;">#import "App.h" @implementation App- (instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; // KVC方法// 等效于以下几句:// self.name = dict[@"name"];// self.icon = dict[@"icon"];// self.title = dict[@"title"];// self.answer = dict[@"answer"]; } return self;} + (instancetype)appWithDict:(NSDictionary *)dict{ return [[self alloc] initWithDict:dict];}@end</span>但是KVC的使用是有条件的 ;
由于KVC会将字典所有的键值对(key-value)赋值给模型对应的属性.只有当
1> 字典的键值对 个数 跟模型的 属性个数 相等
2> 属性名 必须和字典的 键值对 一样
时才可以使用KVC.
<span style="font-size:14px;">/** * 字典转模型,模型再次存入数组中 重写数组的set方法 */-(NSArray *)questions{ if (_questions ==nil) { //加载plist,将plist中存储的字典数据放在数组dictArray中 NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"<span style="color:#FF0000;">questions.plist</span>" ofType:<span style="color:#FF0000;">nil</span>]]; NSMutableArray *questionArray = [NSMutableArray array]; //用自定义的JNQuestion模型question来接收 数组 dictArray 中的元素; for (NSDictionary * dict in dictArray) {<span style="font-size: 12.5px;"></span><pre name="code" class="objc"> Questions *question =[[LeeQuestions alloc]initWithDict:dict];</span>// Questions *question = [LeeQuestions questionWithDict:dict]; [questionArray addObject:question];
} // 赋值 _questions = questionArray; } return _questions; }
0 0
- OC学习 ----- 字典转模型
- oc的字典转模型
- oc 字典转模型的思路
- OC开发:字典转模型框架
- OC学习:OC字典NSDictionary
- IOS 学习 字典转模型
- [oc学习日记]字典
- OC学习----字典
- OC字典转模型和数据列表一级缓存
- OC - KeyValue - 根据字典给模型赋值
- Runtime(字典转模型)学习
- OC学习,字典的整理
- OC学习--字典的使用
- OC篇学习-字典
- OC学习日记09-字典
- 字典转模型时,字典包含字典
- 自定义模型转字典,字典转模型
- Snail—OC学习之字典NSDictionary
- Linux环境编程资源总结
- Python下配置LibSVM
- python 项目 Memory game
- 整数转化为整数字符串
- java中switch的变量类型
- OC学习 ----- 字典转模型
- JDK之jstat的用法
- ubuntu上搭建ntp时间服务器
- 学习心得三
- 数据库相关(2)
- 解开Android应用程序组件Activity的"singleTask"之谜
- 【备忘】迁移 redmine
- Xcode6 IOS开发UITableView基于Storyboard的使用
- Java学习--(四)新的数据类型:类(class);field,method