ios学习8_KVC和字典转模型
Key Value Coding是cocoa的一个标准组成部分,它能让我们能够通过name(key)的方式訪问属性,某些情况下极大地简化了代码。可称之为cocoa的大招。
例如以下的样例:
使用KVC的优点
不使用KVC
- (id)tableView:(NSTableView *)tableview
objectValueForTableColumn:(id)column row:(NSInteger)row {
ChildObject *child = [childrenArray objectAtIndex:row];
if ([[column identifier] isEqualToString:@"name"]) {
return [child name];
}
if ([[column identifier] isEqualToString:@"age"]) {
return [child age];
}
if ([[column identifier] isEqualToString:@"favoriteColor"]) {
return [child favoriteColor];
}
// And so on.
}
使用KVC
- (id)tableView:(NSTableView *)tableview
objectValueForTableColumn:(id)column row:(NSInteger)row {
ChildObject *child = [childrenArray objectAtIndex:row];
return [child valueForKey:[column identifier]];
}
显而易见,简化了非常多代码。
KVC操作
KVC赋值
1 给当前对象属性赋值
- (void)setValue:(id)value forKey:(NSString *)key;
2给对象的属性的属性赋值
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath;
3 处理没有定义的键
- (void) setValue:(id)value forUndefinedKey:(NSString *)key
4 字典转模型:会为我们把和dictionary的key名字同样的class proerty设置上dict中key相应的value
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues;
注意:要求字典中的key和对象属性一样。都是主要的OC数据类型:Array/Dictionary/Boolean/Data/Number/String
KVC取值
1 获取对象属性的值
- (id)valueForKey:(NSString *)key;
2 获取对象属性的属性的值
- (id)valueForKeyPath:(NSString *)keyPath;
样例:
Person * p = [[Person alloc]init];
Car *car = [[Car alloc]init];
p.car = car;
[p setValue:@"qhyuan" forKeyPath:@"name"];
[p setValue:@(20) forKey:@"id"];
[p setValue:@"baoshijie" forKeyPath:@"car.brand"];
[p setValue:@"180000" forKeyPath:@"car.price"];
NSLog(@"kvc賦值的person对象----%@",p);
NSString * name = [p valueForKey:@"name"];
NSString * brand = [p valueForKeyPath:@"car.brand"];
NSLog(@"%@ %@",name, brand);
字典转模型
常规情况
模型
Person.h
@interface Person : NSObject
@property (nonatomic, copy) NSString * name;
@property (nonatomic, assign) int age;
- (instancetype) initWithDict:(NSDictionary *) dict;
+ (instancetype) personWithDict:(NSDictionary *) dict;
+ (NSArray *) person;
@end
Person.m
@implementation Person
- (instancetype) initWithDict:(NSDictionary *) dict
{
if(self = [self init])
{
// 使用KVC 字典转模型 如此方便。省去了大量的赋值代码
[self setValuesForKeysWithDictionary:dict];
//self.name = dict[@"name"];
//self.age = [dict[@"age"] integerValue];
}
return self;
}
+ (instancetype) personWithDict:(NSDictionary *) dict
{
return [[self alloc]initWithDict:dict];
}
+ (NSArray *) person
{
NSMutableArray * mutablePersons = [NSMutableArray array];
NSString * path = [[NSBundle mainBundle] pathForResource:@"persons.plist" ofType:nil];
NSArray *persons = [[NSArray alloc] initWithContentsOfFile:path];
for (NSDictionary * person in persons) {
[mutablePersons addObject:[self personWithDict:person]];
}
return mutablePersons;
}
- (NSString *) description
{
NSString * desc = [NSString stringWithFormat:@"<%p:(%@,%d)>",self,self.name,self.age];
return desc;
}
@end
字典中多个某些key是OC中的keyword
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
假设将键age换成了id
会抛出异常:
*** Terminating app due to uncaught exception 'NSUnknownKeyException',reason: '[<Person 0x8c419a0> setValue:forUndefinedKey:]: this class isnot key value coding-compliant for the key id.
重写下面方法就可以,处理没有定义的键
- (void)setValue:(id)value forUndefinedKey:(NSString *)key;
解决方案:
- (void) setValue:(id)value forUndefinedKey:(NSString *)key
{
if([key isEqualToString:@"id"])
key = @"age";
[super setValue:value forKey:key];
}
字典里面还包括某些相应自己定义类的字典或者数组
Person类添加了一个Car类型的属性
@property (nonatomic, strong) Car * car;
我们仅仅须要重写下面方法
- (void)setValue:(id)value forKey:(NSString *)key;
解决方法:
- (void)setValue:(id)value forKey:(NSString *)key
{
if([key isEqualToString:@"cars"])
{
Car *car = [Car carWithDict:(NSDictionary *)value];
self.car = car;
}
else
[super setValue:value forKey:key];
}
打印结果
字典转模型[5525:60b] (
"<Person:(zhangsan,20,<Car:(benchi,180000)>)>",
"<Person:(lisi,22,<Car:(baoma,180000)>)>",
"<Person:(wangwu,24,<Car:(aodi,180000)>)>"
)
假设不仅仅是添加了Cars属性而是添加了Cars数组,也是类似的方式。
ios学习8_KVC和字典转模型的更多相关文章
- iOS开发UI篇—字典转模型
iOS开发UI篇—字典转模型 一.能完成功能的“问题代码” 1.从plist中加载的数据 2.实现的代码 // // LFViewController.m // 03-应用管理 // // Creat ...
- iOS 懒加载 字典转模型
>>>懒加载 一.介绍 懒加载又称延时加载,即在系统调用时加载,如果系统不调用则不会加载,所谓懒加载其实就是重写其get方法. 在使用懒加载时要先判断该方法是否存在,如果不存在再进行 ...
- 字典转模型框架 Mantle的使用:国外程序员最常用的iOS模型
Mantle简介 Mantle 是iOS和Mac平台下基于Objective-C编写的一个简单高效的模型层框架. Mantle能做什么 Mantle可以轻松把JSON数据.字典(Dictionary) ...
- iOS开发—字典转模型,KVC设计模式
iOS开发UI基础—字典转模型 开发中,通常使用第三方框架可以很快的实现通过字典转模型,通过plist创建模型,将字典的键值对转成模型属性,将模型转成字典,通过模型数组来创建一个字典数组,通过字典数组 ...
- iOS学习笔记38-MJExtension使用
一.MJExtension第三方框架 我们在iOS开发过程中,我们常常需要将字典数据(也就是JSON数据)与Model模型之间的转化,例如网络请求返回的微博数据.等等,如果我们自己全部手动去创建模型并 ...
- ios开发runtime学习五:KVC以及KVO,利用runtime实现字典转模型
一:KVC和KVO的学习 #import "StatusItem.h" /* 1:总结:KVC赋值:1:setValuesForKeysWithDictionary实现原理:遍历字 ...
- ios开发网络学习二:URL转码以及字典转模型框架MJExtension的使用
一:url转码,当url中涉及到中文的时候,要考虑转码,用UTF8对中文的url进行转码 #import "ViewController.h" @interface ViewCon ...
- iOS开发——高级技术精选OC篇&Runtime之字典转模型实战
Runtime之字典转模型实战 如果您还不知道什么是runtime,那么请先看看这几篇文章: http://www.cnblogs.com/iCocos/p/4734687.html http://w ...
- iOS开发——网络篇——JSON和XML,NSJSONSerialization ,NSXMLParser(XML解析器),NSXMLParserDelegate,MJExtension (字典转模型),GDataXML(三方框架解析XML)
一.JSON 1.JSON简介什么是JSONJSON是一种轻量级的数据格式,一般用于数据交互服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) JSON的格式很像OC中的字典 ...
随机推荐
- Java常用工具类---IP工具类、File文件工具类
package com.jarvis.base.util; import java.io.IOException;import java.io.InputStreamReader;import jav ...
- python练习1 登录和三级菜单
,: username1 = input("请输入您的用户名:")# password1 = getpass.getpass("请输入您的密码:") passw ...
- 微信小程序图片上传java后台(前后端代码)
小程序代码 upload:function(e){ var that = this; wx.showActionSheet({ itemList: ['从相册选择','拍照'], itemColor: ...
- webservice和一般处理程序
一丶WebService 1.新建项目 2.选择Web窗体 3.添加新建项 二丶一般处理程序 前台访问: $.ajax({ type: "post", url: "Han ...
- shell learning note
shell learning note MAIN="/usr/local/" # 变量大写 STATUS="$MAIN/status" # 美元符加字符串是 ...
- linux下查找字符串的命令
1. set命令可以显示出当前shell下所有全局参量定义及其值; 2. 查找并删除当前目录下小文件: find . -type f -size -10k -exec rm {} \; 说明: w ...
- linux中查看文件指定行的数据
http://jingyan.baidu.com/article/15622f24125872fdfdbea560.html
- PHP典型功能与Laravel5框架开发学习笔记
步骤一:PHP的Redis应用及HTTP协议 一.Redis初识 1.Linux下安装redis:具体看官网:https://redis.io/download:以下为以个人习惯的安装目录进行的red ...
- LeetCode(43)Multiply Strings
题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Note: ...
- [转]ionic或者angularjs中图片显示压缩问题解决 or 显示较大图片的某一块区域、裁剪显示
我们知道在html中显示图片一般都是用img控件标签,当然调整大小的也很容易. 但是会出现,特定的img大小,显示一张比较大尺寸的且长宽比例与特定img大小不相符的图片.而导致压缩问题,图片挤压的很严 ...