KVC示例

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

KVC –key value Coding,可以让我们通过键值编码的形式进行属性值的赋值

参考苹果官网的图。。

1.KVC

定义一个Person类

.h文件

1:#import <Foundation/Foundation.h>

2:

3:@interfacePerson : NSObject

4:{

5:NSString *name;

6:}

7:

8:@end

.csharpcode, .csharpcodePRe

{

font-size: small;

color: black;

font-family: consolas, "Courier New", courier, monospace;

background-color: #ffffff;

/*white-space: pre;*/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp{ background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }.m文件

1:#import"Person.h"

2:

3:@implementationPerson

4:

5:@end

.csharpcode, .csharpcode pre

{

font-size: small;

color: black;

font-family: consolas, "Courier New", courier, monospace;

background-color: #ffffff;

/*white-space: pre;*/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }

通过普通的获取属性的方法是无法对name进行赋值和取值操作的,这种情况可以通过KVC来进行设置和取值

1:- (void)viewDidLoad {

2:[super viewDidLoad];

3:// Do any additional setup after loading the view, typically from a nib.

4:Person person=[[Person alloc]init];

5:[person setValue:@"keith"forKey:@"name"];

6:NSLog(@"name is %@",[person valueForKey:@"name"]);

7:}

输出:

2014-11-26 21:51:12.237 KVCtest[543:10453] name is keith

.csharpcode, .csharpcode pre

{

font-size: small;

color: black;

font-family: consolas, "Courier New", courier, monospace;

background-color: #ffffff;

/*white-space: pre;*/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }赋值用setValue:forKey:

取值用 valueForKey:

2.键路径

如果类中包含其他的类怎么办呢,访问这个类的属性怎么办呢。可以使用setValue: forKeyPath: 和valueForKeyPath设置和获取

如定义一个car

.h文件

#import<Foundation/Foundation.h>@interfaceCar : NSObject

{

NSString*carname;

}@end

将Person的.h改成

#import<Foundation/Foundation.h>@classCar;@interfacePerson : NSObject

{

NSString*name;

Car*hiscar;

}-(id)init;@end

.m为

#import"Person.h"#import"Car.h"@implementationPerson-(id)init{if(self=[super init]) {

hiscar=[[Car alloc]init];

}returnself;

}@end

这里实现了car的实例化,如果不这么做在键值编程时候hiscar因为没有初始化而出现无法赋值的情况

调用:

- (void)viewDidLoad {

[super viewDidLoad];//Do any additional setup after loading the view, typically from a nib.Person *person=[[Person alloc]init];

[person setValue:@"keith"forKey:@"name"];

[person setValue:@"QQ"forKeyPath:@"hiscar.carname"];

NSLog(@"name is %@,hiscar is %@",[person valueForKey:@"name"],[person valueForKeyPath:@"hiscar.carname"]);/*或者这样*/Car*car=[[Car alloc]init];

[car setValue:@"BeiKy"forKey:@"carname"];

[person setValue:car forKey:@"hiscar"];

NSLog(@"name is %@,hiscar is %@",[person valueForKey:@"name"],[person valueForKeyPath:@"hiscar.carname"]);

}

输出结果

2014-11-26 22:45:47.392 KVCtest[1031:25564] name is keith,hiscar is QQ

2014-11-26 22:45:47.394 KVCtest[1031:25564] name is keith,hiscar is BeiKy

3.类型封装和转换

再次修改Person.h文件

#import<Foundation/Foundation.h>@classCar;@interfacePerson : NSObject

{

NSString*name;

Car*hiscar;

NSInteger age;//增加一个年龄类型是NSInteger}-(id)init;@end

那么如何在设置的时候会发生什么呢?

- (void)viewDidLoad {

[super viewDidLoad];//Do any additional setup after loading the view, typically from a nib.Person *person=[[Person alloc]init];

[person setValue:@"keith"forKey:@"name"];

[person setValue:@"QQ"forKeyPath:@"hiscar.carname"];

[person setValue:@"24"forKeyPath:@"age"];//为年龄赋值NSLog(@"name is %@, is %@ old ,hiscar is %@",[person valueForKey:@"name"],[person valueForKey:@"age"],[person valueForKeyPath:@"hiscar.carname"]);

}

结果是:

2014-11-26 22:56:08.873 KVCtest[1086:27956] name is keith, is 24 old ,hiscar is QQ

4.操作集合

在Person中添加一个otherPerson

#import<Foundation/Foundation.h>@classCar;@interfacePerson : NSObject

{

NSString*name;

Car*hiscar;

NSInteger age;

NSMutableArray*otherPerson;

}-(id)init;@end

属性.@sum.属性,还有@count,@avg,@max.@min

-(void)kvcCollection{

Person*person=[[Person alloc]init];

[person setValue:@"keith"forKey:@"name"];

Person*person1=[[Person alloc]init];

Person*person2=[[Person alloc]init];

Person*person3=[[Person alloc]init];

Person*person4=[[Person alloc]init];

NSMutableArray*others=[[NSMutableArray alloc]initWithObjects:person1,person2,person3,person4, nil];

NSInteger agenum=20;for(Person *pinothers) {

[self setage:p age:[[NSString alloc]initWithFormat:@"%ld",agenum++]];

}

[person setValue:others forKey:@"otherPerson"];

NSLog(@"other's age is %@",[person valueForKeyPath:@"otherPerson.age"]);

NSLog(@"the avg age is %@",[person valueForKeyPath:@"otherPerson.@avg.age"]);

NSLog(@"the sum age is %@",[person valueForKeyPath:@"otherPerson.@sum.age"]);

NSLog(@"the count person is %@",[person valueForKeyPath:@"otherPerson.@count.age"]);

NSLog(@"the min age is %@",[person valueForKeyPath:@"otherPerson.@min.age"]);

NSLog(@"the max age is %@",[person valueForKeyPath:@"otherPerson.@max.age"]);

}-(void)setage:(Person *)person age:(NSString *)age{

[person setValue: age forKey:@"age"];

}

输出

2014-11-26 23:51:00.180 KVCtest[1346:42300] other's age is (

20,

21,

22,

23

)

2014-11-26 23:51:00.183 KVCtest[1346:42300] the avg age is 21.5

2014-11-26 23:51:00.183 KVCtest[1346:42300] the sum age is 86

2014-11-26 23:51:00.184 KVCtest[1346:42300] the count person is 4

2014-11-26 23:51:00.184 KVCtest[1346:42300] the min age is 20

2014-11-26 23:51:00.184 KVCtest[1346:42300] the max age is 23

本文参考容芳志博客,感谢这位博友

著作权声明:本文由http://www.cnblogs.com/keithmoring/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航