KVC示例
KVC –key value Coding,可以让我们通过键值编码的形式进行属性值的赋值
参考苹果官网的图。。
1.KVC
定义一个Person类
.h文件
1: #import <Foundation/Foundation.h>
2:
3: @interface Person : NSObject
4: {
5: NSString *name;
6: }
7:
8: @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; }
.m文件
1: #import "Person.h"
2:
3: @implementation Person
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: }
输出:
.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> @interface Car : NSObject
{
NSString *carname;
}
@end
将Person的.h改成
#import <Foundation/Foundation.h>
@class Car;
@interface Person : NSObject
{
NSString *name;
Car *hiscar;
}
-(id)init;
@end
.m为
#import "Person.h"
#import "Car.h"
@implementation Person
-(id)init{
if (self=[super init]) {
hiscar=[[Car alloc]init];
}
return self;
}
@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>
@class Car;
@interface Person : 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>
@class Car;
@interface Person : 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 *p in others) {
[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/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!
KVC示例的更多相关文章
- iOS - 详细理解KVC与KVO
详细理解KVC与KVO 在面试的时候,KVC与KVO有些时候还是会问到的,并且他们都是Objective C的关键概念,在这里我们先做一个简单地介绍: (一)KVC: KVC即指:NSKeyValue ...
- ObjC 巧用反射和KVC实现JSON快速反序列化成对象
1.简单的KVC介绍 KVC是一种间接访问对象属性的机制,不直接调用getter 和 setter方法,而使用valueForKey 来替代getter 方法,setValue:forKey来代替se ...
- iOS开发——UI基础-KVC
除了一般的赋值和取值的方法,我们还可以用Key-Value-Coding(KVC)键值编码来访问你要存取的类的属性. 如何使用KVC存取对象属性呢?看个示例 一.使用KVC存数据 定义一个person ...
- iOS开发UI篇—Kvc简单介绍
ios开发UI篇—Kvc简单介绍 一.KVC简单介绍 KVC key valued coding 键值编码 KVC通过键值间接编码 补充: 与KVC相对的时KVO,即key valued observ ...
- Key-Value-Coding(KVC)
Objective-C语法之KVC的使用 除了一般的赋值和取值的方法,我们还可以用Key-Value-Coding(KVC)键值编码来访问你要存取的类的属性. 下图来自苹果官网: 如何使用KVC存 ...
- KVC笔记
利用KVC可以随意修改一个对象的属性或者成员变量(并且私有的也可以修改) 示例代码: 示例一: Person *p = [[Person alloc] init]; p.dog = [[Dog al ...
- iOS开发技巧系列---详解KVC(我告诉你KVC的一切)
KVC(Key-value coding)键值编码,单看这个名字可能不太好理解.其实翻译一下就很简单了,就是指iOS的开发中,可以允许开发者通过Key名直接访问对象的属性,或者给对象的属性赋值.而不需 ...
- KVC/KVO总结
KVC(键值编码) 动态设置: setValue:属性值 forKey:属性名(用于简单路径) setValue:属性值 forKeyPath:属(用于复合路径,例如Person有一个Account类 ...
- iOS开发—字典转模型,KVC设计模式
iOS开发UI基础—字典转模型 开发中,通常使用第三方框架可以很快的实现通过字典转模型,通过plist创建模型,将字典的键值对转成模型属性,将模型转成字典,通过模型数组来创建一个字典数组,通过字典数组 ...
随机推荐
- win7系统扩展双屏幕时,怎样在两个屏幕下都显示任务栏
扩展屏幕下都显示任务栏!!! win7系统本身无法设置该功能(眼下我是不知道) 但能够下载第三方软件来解决该问题. 第一步:Dual Monitor Taskbar 下载软件 下载链接:http:// ...
- 正则化--L1正则化(稀疏性正则化)
稀疏矢量通常包含许多维度.创建特征组合会导致包含更多维度.由于使用此类高维度特征矢量,因此模型可能会非常庞大,并且需要大量的 RAM. 在高维度稀疏矢量中,最好尽可能使权重正好降至 0.正好为 0 的 ...
- rxjs1
<li *ngFor="let fruit of fruitsList; let i = index;">{{i}}-{{fruit.name}}-{{fruit.pr ...
- 自己实现strcpy函数
#include //printf #include //ssert #include //malloc #include //strlen char * sstrcpy(char * strdst, ...
- iOS开发多线程篇 10 —NSOperation基本操作
iOS开发多线程篇—NSOperation基本操作 一.并发数 (1)并发数:同时执⾏行的任务数.比如,同时开3个线程执行3个任务,并发数就是3 (2)最大并发数:同一时间最多只能执行的任务的个数. ...
- spring boot 多层级mapper
mapper目录结构: mapper ----dev -------produce 在 application.properties 文件中配置 mybatis.mapper-location ...
- 定时器(setTimeout/setInterval)调用带参函数失效解决方法
也许你曾碰到过这样的问题,不管是setInterval()还是setTimeout(),当code参数里放一个带参函数时,定时器都会失效,看下面这个例子: function test(str){ al ...
- dropdown多选下拉框
写好了一个dropdown多选框.直接粘下面代码就能用 效果展示: temp2.jsp <%@page import="com.util.LabelCacheManager" ...
- gen_server边缘
我们以Module代表gen_server的callback模块 1, 实现gen_server behaviour的模块会产生一个新的process么? 毫无疑问,太会了!通过调用proc_lib: ...
- 【转】Monkey测试6-Monkey Test Log
Moneky Test Log 分析: 首先用一个最简单的例子分析:monkey --pct-trackball 0 --throttle 100 -v 500/*p参数: 表示指定测试的程序/*v参 ...