[转]字典的快速赋值 setValuesForKeysWithDictionary

前言

在学习解析数据的时候,我们经常是这么写的:
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"];

输出:

        NSLog(@"test.name=%@",test.name);
        NSLog(@"test.sex=%@",test.sex);
        NSLog(@"test.age=%@",test.age);

输出结果:

2015-10-19 13:31:25.478
setValuesForKeysWithDictionary[9676:913009] test.name=张三
2015-10-19 13:31:25.478
setValuesForKeysWithDictionary[9676:913009] test.sex=男
2015-10-19 13:31:25.478
setValuesForKeysWithDictionary[9676:913009] test.age=22

看上去很有条理,按部就班,但是一旦数据多了起来,却会非常繁琐,所以这次我会介绍一个相对轻松的方法setValuesForKeysWithDictionary。


简单使用

如果用setValuesForKeysWithDictionary这个方法会怎样?
将赋值过程

  test.name=dic[@"name"];
    test.sex=dic[@"sex"];
    test.age=dic[@"age"];

替换为一句话

    [test setValuesForKeysWithDictionary:dic];

输出结果一模一样,是不是简单又方便?


深入的问题

  1. 如果model里面的有不存在于dic中的元素会怎样?

在Model文件中添加一行

    @property (nonatomic,copy)NSString *other;

并输出得时候输出

     NSLog(@"test.other=%@",test.other);

输出结果:

2015-10-19 13:49:25.955
setValuesForKeysWithDictionary[9964:928391] test.name=张三
2015-10-19 13:49:25.956
setValuesForKeysWithDictionary[9964:928391] test.sex=男
2015-10-19 13:49:25.956
setValuesForKeysWithDictionary[9964:928391] test.age=22
2015-10-19 13:49:25.956
setValuesForKeysWithDictionary[9964:928391] test.other=(null)

显而易见,dic中得值可以完全赋值给model,而other没有被赋值,所以值是空的。



2.如果dic里面的有不存在于model中的元素会怎样?

在Model文件中删除一行

    @property (nonatomic,copy) NSString* age;

在删除对应得输出后运行。

糟了!通过了编译,但是运行时报错!

Terminating app due to uncaught exception ‘NSUnknownKeyException‘,
reason: ‘[<PersonModel 0x7fd731517910> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key age.‘

因为在model中,没有对应的age属性,所以导致了程序崩溃。

解决方式就是实现一个方法setValue:forUndefinedKey: 这个方法能过滤掉不存在的键值。

在model中添加。
h文件中添加:

    -(void)setValue:(id)value forUndefinedKey:(NSString *)key;

并需要在m文件中实现:

    -(void)setValue:(id)value forUndefinedKey:(NSString *)key{

    }

对,并不需要在方法中写任何内容。
现在来运行一下。
输出结果:

2015-10-19 13:55:55.390
setValuesForKeysWithDictionary[10082:937173] test.name=张三
2015-10-19 13:55:55.391
setValuesForKeysWithDictionary[10082:937173] test.sex=男

成功运行!



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

运行后结果:

    2015-10-19 14:30:11.241
    setValuesForKeysWithDictionary[10289:956012] test.name=张三
    2015-10-19 14:30:11.242
    setValuesForKeysWithDictionary[10289:956012] test.sex=男
    2015-10-19 14:30:11.242
    setValuesForKeysWithDictionary[10289:956012] test.age=22

 

时间: 2024-12-24 05:01:38

[转]字典的快速赋值 setValuesForKeysWithDictionary的相关文章

字典的快速赋值 setValuesForKeysWithDictionary

字典的快速赋值 setValuesForKeysWithDictionary ? 前言 在学习解析数据的时候,我们经常是这么写的:PersonModel.h文件中    @property (nonatomic,copy)NSString *name;    @property (nonatomic,copy)NSString *sex;    @property (nonatomic,copy)NSString *age; 字典:     NSDictionary *dic = @{@"nam

【iOS开发】字典的快速赋值 setValuesForKeysWithDictionary

前言 在学习解析数据的时候,我们经常是这么写的:PersonModel.h文件中 @property (nonatomic,copy)NSString *name; @property (nonatomic,copy)NSString *sex; @property (nonatomic,copy)NSString *age; 字典: NSDictionary *dic = @{@"name":@"张三",@"sex":@"男"

用字典给Model赋值

此篇教程讲述通过runtime扩展NSObject,可以直接用字典给Model赋值,这是相当有用的技术呢. 源码: NSObject+Properties.h 与 NSObject+Properties.m // // NSObject+Properties.h // // Created by YouXianMing on 14-9-4. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <Foundatio

iOS开发之--字典快速赋值

以往在学习解析数据的时候,我们用的方法都是一个一个生命,然后加到字典里面,然后进行复制,那样的麻烦,而且也不能保证一次成功,不出错,我是遇到过多次key值的问题! 其实可以把复制的过程替换成一句话: [test setValuesForKeysWithDictionary:dic]; 问题一:model里面有不存在与dic中的元素会怎样? 这个时候,在控制台输出为空"=(null)" 问题二:如果字典当中有不存在与model中的元素会怎样? 会出错,或者崩溃,那是因为在model中,没

用字典给Model赋值并支持map键值替换

这个是昨天教程的升级版本,支持键值的map替换. 源码如下: NSObject+Properties.h 与 NSObject+Properties.m // // NSObject+Properties.h // // Created by YouXianMing on 14-9-4. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> @interface

.NET通过字典给类赋值

1 /// <summary> 2 /// 3 /// </summary> 4 /// <typeparam name="T"></typeparam> 5 /// <param name="origin">源数据</param> 6 /// <param name="target">对象数据</param> 7 /// <param name

用字典给Model(继承与NSObject的对象)赋值---------【iOS 开发】

此篇教程讲述通过runtime扩展NSObject,可以直接用字典给Model赋值,这是相当有用的技术呢. PS:关于runtime的详情以后会详细介绍 源码: NSObject+Property.h 与 NSObject+Property.m // // NSObject+Property.h // ModelValueTest1.0 // // Created by Lisa on 14-9-15. // Copyright (c) 2014年 Lisa. All rights reserv

OC --(5)-- 字典、集、数组排序:字典类、集合类、数组数组排序、字典、集合的快速遍历、数组排序

字典 1.快速枚举 forin   运行时不能更改内部数据 for (<#type *object#> in <#collection#>) { <#statements#> }         //对象                    //数组 for (Contact *contact in friends) {  [contact show];  } 判定是否为空  NSString *ns ==nil      NSInteger inte==0 一.字典

【学习ios之路:Object-C】字典.集合.

1.不可变数据字典(NSDicionary) 字典:用来存储具有一一对应关系的数据. 一个key 对应一个 value ,key起到了唯一标示的作用,key必须是唯一的,但是一个vlaue可以对应多个key. 字典存储的是无序的元素,一对键值对组成了字典中的一个元素. ①.不可变字典初始化赋值 <span style="font-size:18px;"> //笑笑语法 NSDictionary *dic = @{@"name":@"zhangd