KVC,即是指 NSKeyValueCoding,提供一种机制来间接访问对象的属性。常用于字典转模型,或者用于模型转字典,继承于NSObject 类,子类可用直接使用。
//可以给当前对象属性赋值 - (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"]。
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues;
注意:要求字典中的key 和对象属性要求一样
- (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]]); }
//只能获取对象属性的值 - (id)valueForKey:(NSString *)key; //可以获取对象属性的属性的值(推荐使用) - (id)valueForKeyPath:(NSString *)keyPath;
KVC 可用进行简单的运算,例如 sum avg min 的运算。
[array valueForKeyPath:@"@sum.age"];
NSLog(@"sum=%@",[self.dataArray valueForKeyPath:@"@sum.age"]); NSLog(@"avg=%@",[self.dataArray valueForKeyPath:@"@avg.age"]); NSLog(@"min=%@",[self.dataArray valueForKeyPath:@"@min.age"]);
KVC需要把字符串属性名称转换后才能赋值,调用的set方法,性能消耗较高
#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
===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
===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
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