字典的快速赋值model: setValuesForKeysWithDictionary
2016-08-17 16:44
435 查看
以前:直接赋值
PersonModel.h文件中
字典:
赋值:
现在:用setValuesForKeysWithDictionary
产生问题
如果model里面的有不存在于dic中的元素会怎样?
在Model文件中添加一行
并输出得时候输出:test.other=(null)
dic中得值可以完全赋值给model,而other没有被赋值,所以值是空的。
2.如果dic里面的有不存在于model中的元素会怎样?
在Model文件中删除一行
在删除对应得输出后运行。
崩溃!
因为在model中,没有对应的age属性,所以导致了程序崩溃。
解决方式就是实现一个方法setValue:forUndefinedKey: 这个方法能过滤掉不存在的键值。
在model中添加。
h文件中添加:
并需要在m文件中实现:
3.如果dic中的key与model中的变量名字不同,应该怎么赋值?
从前面我们可以知道,dic中key赋值给model中与key同名的属性。
那么如果dic中得key值为 username,model中的名字为name,又或是dic中的key值为ID,INT 等关键字,应该怎么变化。
答案也是从setValue:forUndefinedKey方法入手。
首先我们把dic的值改变:
model中的属性:
完善model中的setValue:forUndefinedKey方法
文/嘻嘻zhy(简书作者)
原文链接:http://www.jianshu.com/p/870eb4b4170a
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
PersonModel.h文件中
@property (nonatomic,copy)NSString *name; @property (nonatomic,copy)NSString *sex; @property (nonatomic,copy)NSString *age;
字典:
NSDictionary *dic = @{@"name":@"张三",@"sex":@"男",@"age":@"22"};
赋值:
PersonModel *test=[[PersonModel alloc]init]; test.name=dic[@"name"]; test.sex=dic[@"sex"]; test.age=dic[@"age"];
现在:用setValuesForKeysWithDictionary
[test setValuesForKeysWithDictionary:dic];
产生问题
如果model里面的有不存在于dic中的元素会怎样?
在Model文件中添加一行
@property (nonatomic,copy)NSString *other;
并输出得时候输出:test.other=(null)
dic中得值可以完全赋值给model,而other没有被赋值,所以值是空的。
2.如果dic里面的有不存在于model中的元素会怎样?
在Model文件中删除一行
@property (nonatomic,copy) NSString* age;
在删除对应得输出后运行。
崩溃!
因为在model中,没有对应的age属性,所以导致了程序崩溃。
解决方式就是实现一个方法setValue:forUndefinedKey: 这个方法能过滤掉不存在的键值。
在model中添加。
h文件中添加:
-(void)setValue:(id)value forUndefinedKey:(NSString *)key;
并需要在m文件中实现:
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{ }
3.如果dic中的key与model中的变量名字不同,应该怎么赋值?
从前面我们可以知道,dic中key赋值给model中与key同名的属性。
那么如果dic中得key值为 username,model中的名字为name,又或是dic中的key值为ID,INT 等关键字,应该怎么变化。
答案也是从setValue:forUndefinedKey方法入手。
首先我们把dic的值改变:
NSDictionary *dic = @{@"username":@"张三",@"sex":@"男",@"id":@"22"};
model中的属性:
@property (nonatomic,copy)NSString *name; @property (nonatomic,copy)NSString *sex; @property (nonatomic,copy) NSString* age;
完善model中的setValue:forUndefinedKey方法
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{ if([key isEqualToString:@"id"]) { self.age=value; } if([key isEqualToString:@"username"]) { self.name=value; } }
文/嘻嘻zhy(简书作者)
原文链接:http://www.jianshu.com/p/870eb4b4170a
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
相关文章推荐
- 字典的快速赋值 setValuesForKeysWithDictionary
- iOS 字典的快速赋值 setValuesForKeysWithDictionary
- 字典的快速赋值 setValuesForKeysWithDictionary
- 字典的快速赋值 setValuesForKeysWithDictionary
- 字典的快速赋值 setValuesForKeysWithDictionary
- 5、关于kvc中的setValuesForKeysWithDictionary为model赋值
- 【iOS】关于kvc中的setValuesForKeysWithDictionary为model赋值的一些常见知识
- iOS -- 给model赋值时走了[self setValuesForKeysWithDictionary:dic]不走setvalue: forked:
- [置顶] iOS开发setValuesForKeysWithDictionary生成model解析神器
- iOS setValuesForKeysWithDictionary
- setValuesForKeysWithDictionary方法的正确使用姿势
- setValuesForKeysWithDictionary 使用方法
- setValuesForKeysWithDictionary:的用途
- setValuesForKeysWithDictionary的妙用
- setValuesForKeysWithDictionary forUndefinedKey
- iOS KVC setValuesForKeysWithDictionary的使用
- iOS 开发之 KVC - setValuesForKeysWithDictionary 解析神器
- setValuesForKeysWithDictionary
- 自定义实现setValuesForKeysWithDictionary
- swift 调用 setValuesForKeysWithDictionary 异常问题