[转]KVC/KVO

 

 

目录[-]

  • 一 KVC简介
  • 二 KVC赋值
  • 二 字典转模型
  • 三 模型转字典
  • 四 KVC取值
  • 五 KVC运算
  • 六 注意事项
  • 七 代码
  • 7.1 ViewController
  • 7.2Cat类
  • 7.3 person类
  • 7.4 输出

一 KVC简介

KVC,即是指 NSKeyValueCoding,提供一种机制来间接访问对象的属性。常用于字典转模型,或者用于模型转字典,继承于NSObject 类,子类可用直接使用。

 

 

二 KVC赋值

1
2
3
4
5
6
7
8
//可以给当前对象属性赋值
- (void)setValue:(id)value forKey:(NSString *)key;
 
//可以给对象的属性的属性赋值(推荐使用)
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath;
 
//字典转对象
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues;

 

 

注意:

forKey只能给当前对象属性赋值。

例如:[self setVale:@"wlx" forkey:@"name"]。

 

forKeyPath 可以给对象的属性的属性赋值。

例如:[self setVale:@"thinkpad" forkeypath:@"person.computer"]。

 

 

二 字典转模型

1
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues;

 

注意:要求字典中的key 和对象属性要求一样

 

 

三 模型转字典

1
2
3
4
5
6
7
- (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys;
 
//实例:
NSDictionary *dic = [self.dataArray dictionaryWithValuesForKeys:@[@"name",@"age"]];
for (int i =0 ; i<dic.count; i++) {
    NSLog(@"%@", [dic objectForKey:[dic allKeys][i]]);
}

 

 

 

四 KVC取值

1
2
3
4
5
//只能获取对象属性的值
- (id)valueForKey:(NSString *)key;
 
//可以获取对象属性的属性的值(推荐使用)
- (id)valueForKeyPath:(NSString *)keyPath;

 

 

 

五 KVC运算

KVC 可用进行简单的运算,例如 sum avg min 的运算。

[array valueForKeyPath:@"@sum.age"];

1
2
3
NSLog(@"sum=%@",[self.dataArray valueForKeyPath:@"@sum.age"]);
NSLog(@"avg=%@",[self.dataArray valueForKeyPath:@"@avg.age"]);
NSLog(@"min=%@",[self.dataArray valueForKeyPath:@"@min.age"]);

 

 

 

六 注意事项

KVC需要把字符串属性名称转换后才能赋值,调用的set方法,性能消耗较高

 

 

七 代码

7.1 ViewController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#import "ViewController.h"
#import "Cat.h"
#import "Person.h"
 
 
@interface ViewController ()
@property (nonatomic,strong) NSArray *dataArray;
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
     
    /** KVC 测试 **/
     
    //1.KVC 设置Cat 值
    NSLog(@"=====KVC 设置Cat 值  字段转模型====");
    for (int i=0; i<self.dataArray.count; i++) {
        Cat *cat = (Cat *)self.dataArray[i];
        NSLog(@"%@",cat.name);
    }
     
     
     
    //2 测试 forKey forKeyPath 区别
    NSLog(@"=====forKey forKeyPath valueForKey valueForKeyPath 区别 ====");
    //2.1 创建 person 对象
    Person *p = [[Person alloc]init];
    //2.2 创建 cat 对象
    Cat *cat = self.dataArray[0];
    //2.3 把 cat 赋值给 persion 对象中的 cat 属性
    p.cat = cat;
     
    //2.4 设置 person name 属性的值
    [p setValue:@"卧龙小" forKey:@"name"];
     
    //2.5 设置 persion cat 对象的 name 属性
    //[p setValue:[cat name] forKey:@"cat.name"]; //报错NSUnknownKeyException
    [p setValue:@"cat4" forKeyPath:@"cat.name"];
     
    NSLog(@"%@ - %@",[p valueForKey:@"name"],[p valueForKeyPath:@"cat.name"]);
     
     
     
     
    //3 模型转字典
    NSLog(@"=====模型转字典 ====");
    NSDictionary *dic = [self.dataArray dictionaryWithValuesForKeys:@[@"name",@"age"]];
    for (int i =0 ; i<dic.count; i++) {
        NSLog(@"%@", [dic objectForKey:[dic allKeys][i]]);
    }
     
     
    //4 kvc运算
    NSLog(@"=====kvc运算====");
    NSLog(@"sum=%@",[self.dataArray valueForKeyPath:@"@sum.age"]);
    NSLog(@"avg=%@",[self.dataArray valueForKeyPath:@"@avg.age"]);
    NSLog(@"min=%@",[self.dataArray valueForKeyPath:@"@min.age"]);
}
 
/**
 *  懒加载
 */
-(NSArray *)dataArray
{
     
    //    1.获取文件
    NSBundle *bundle = [NSBundle mainBundle];
    NSArray *array = [NSArray arrayWithContentsOfFile:[bundle pathForResource:@"cat.plist" ofType:nil]];
     
    NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:array.count];
     
    //    2.封装数据
    for (NSDictionary *dic in array) {
        Cat *cat = [Cat catWithDic:dic];
        [mutableArray addObject:cat];
    }
     
    //    3.返回数据
    _dataArray = mutableArray;
    return _dataArray;
}
@end

 

 

7.2Cat类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
===cat.h===
@interface Cat : NSObject
 
/**
 *  年龄
 */
@property (nonatomic,assign)NSInteger age;
 
/**
 *  名字
 */
@property (nonatomic,copy)NSString *name;
 
 
 
 
/**
 *  构造方法
 *
 *  @param dic <#dic description#>
 *
 *  @return <#return value description#>
 */
-(instancetype)initWithDic:(NSDictionary *)dic;
 
+(instancetype)catWithDic:(NSDictionary *)dic;
 
@end
 
 
===cat.m===
 
#import <Foundation/Foundation.h>
#import "Cat.h"
 
@implementation Cat
-(instancetype)initWithDic:(NSDictionary *)dic
{
    if (self = [super init]) {
         
        /** KVC 字典转模型 **/
        //1 通过 setValue 方法
         
        //[self setValue:dic[@"name"] forKey:@"name"];
        //[self setValue:dic[@"age"] forKeyPath:@"age"];
 
         
        //2 字典转模型
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
 
 
 
+(instancetype)catWithDic:(NSDictionary *)dic
{
    return [[self alloc]initWithDic:dic];
}
@end

 

 

 

7.3 person类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
===person.h===
#import "Cat.h"
 
@interface Person : NSObject
 
 
/**
 *  名字
 */
@property (nonatomic,copy)NSString *name;
 
/**
 *  拥有的猫
 */
@property (nonatomic,strong)Cat *cat;
 
@end
 
 
===person.m===
#import <Foundation/Foundation.h>
#import "Person.h"
 
@implementation Person
@end

 

 

7.4 输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2014-12-29 00:42:03.814 22KVC测试[2484:149139] =====KVC 设置Cat 值  字段转模型====
2014-12-29 00:42:03.815 22KVC测试[2484:149139] cat1
2014-12-29 00:42:03.815 22KVC测试[2484:149139] cat2
2014-12-29 00:42:03.815 22KVC测试[2484:149139] cat3
2014-12-29 00:42:03.816 22KVC测试[2484:149139] =====forKey forKeyPath valueForKey valueForKeyPath 区别 ====
2014-12-29 00:42:03.816 22KVC测试[2484:149139] 卧龙小 - cat4
2014-12-29 00:42:03.816 22KVC测试[2484:149139] =====模型转字典 ====
2014-12-29 00:42:03.816 22KVC测试[2484:149139] (
    cat1,
    cat2,
    cat3
)
2014-12-29 00:42:03.816 22KVC测试[2484:149139] (
    5,
    7,
    5
)
2014-12-29 00:42:03.816 22KVC测试[2484:149139] =====kvc运算====
2014-12-29 00:42:03.816 22KVC测试[2484:149139] sum=17
2014-12-29 00:42:03.817 22KVC测试[2484:149139] avg=5.6666666666666666666666666666666666666
2014-12-29 00:42:03.817 22KVC测试[2484:149139] min=5

源:

http://my.oschina.net/u/1032974/blog/361762?p={{page}}

posted @ 2015-04-22 14:35  沙漠浮萍  阅读(240)  评论(0编辑  收藏  举报