iOS中KVC和KVO
2016-03-29 14:23
351 查看
KVC:键 - 值编码是一种间接访问对象的属性使用字符串来标识属性,而不是通过调用存取方法,直接或通过实例变量访问的机制。
KVO:键值观察机制,他提供了观察某一属性变化的方法,极大的简化了代码。
KVC,即是指 NSKeyValueCoding,一个非正式的
Protocol,提供一种机制来间接访问对象的属性。KVO 就是基于 KVC 实现的关键技术之一。
一个对象拥有某些属性。比如说,一个 Person 对象有一个 name 和一个 address 属性。以 KVC 说法,Person 对象分别有一个 value 对应他的 name 和 address 的 key。 key 只是一个字符串,它对应的值可以是任意类型的对象。从最基础的层次上看,KVC 有两个方法:一个是设置 key 的值,另一个是获取 key 的值。如下面的例子:
void changeName(Person *p, NSString *newName)
{
// using the KVC accessor (getter) method
NSString *originalName = [p valueForKey:@"name"];
// using the KVC accessor (setter) method.
[p setValue:newName forKey:@"name"];
NSLog(@"Changed %@'s name to: %@", originalName, newName);
}
?
现在,如果 Person 有另外一个 key 配偶(spouse),spouse 的 key 值是另一个 Person 对象,用 KVC 可以这样写:
voidlogMarriage(Person *p)
{
// just using the accessor again, same as example above
NSString *personsName = [p valueForKey:@"name"];
// this line is different, because it is using
// a "key path" instead of a normal "key"
NSString *spousesName = [p valueForKeyPath:@"spouse.name"];
NSLog(@"%@ is happily married to %@", personsName, spousesName);
}
?
key 与 key pat 要区分开来,key 可以从一个对象中获取值,而 key path 可以将多个 key 用点号 “.” 分割连接起来,比如:
[p valueForKeyPath:@"spouse.name"];
相当于这样……
[[p valueForKey:@"spouse"] valueForKey:@"name"];
2 KVO(Key-Value Observing)
Key-Value Observing (KVO) 建立在 KVC 之上,它能够观察一个对象的 KVC key path 值的变化。举个例子,用代码观察一个 person 对象的 address 变化,以下是实现的三个方法:
watchPersonForChangeOfAddress: 实现观察
observeValueForKeyPath:ofObject:change:context: 在被观察的 key path 的值变化时调用。
dealloc 停止观察
static NSString *const KVO_CONTEXT_ADDRESS_CHANGED = @"KVO_CONTEXT_ADDRESS_CHANGED"
@implementation PersonWatcher
-(void) watchPersonForChangeOfAddress:(Person *)p
{
// this begins the observing
[p addObserver:self
forKeyPath:@"address"
options:0
context:KVO_CONTEXT_ADDRESS_CHANGED];
// keep a record of all the people being observed,
// because we need to stop observing them in dealloc
[m_observedPeople addObject:p];
}
// whenever an observed key path changes, this method will be called
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
// use the context to make sure this is a change in the address,
// because we may also be observing other things
if(context == KVO_CONTEXT_ADDRESS_CHANGED) {
NSString *name = [object valueForKey:@"name"];
NSString *address = [object valueForKey:@"address"];
NSLog(@"%@ has a new address: %@", name, address);
}
}
-(void) dealloc;
{
// must stop observing everything before this object is
// deallocated, otherwise it will cause crashes
for(Person *p in m_observedPeople){
[p removeObserver:self forKeyPath:@"address"];
}
[m_observedPeople release];
m_observedPeople = nil;
[super dealloc];
}
-(id) init;
{
if(self = [super init]){
m_observedPeople = [NSMutableArray new];
}
return self;
}
@end
这就是 KVO 的作用,它通过 key path 观察对象的值,当值发生变化的时候会收到通知。
KVO:键值观察机制,他提供了观察某一属性变化的方法,极大的简化了代码。
1 Key-Value Coding (KVC)
KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性。KVO 就是基于 KVC 实现的关键技术之一。
一个对象拥有某些属性。比如说,一个 Person 对象有一个 name 和一个 address 属性。以 KVC 说法,Person 对象分别有一个 value 对应他的 name 和 address 的 key。 key 只是一个字符串,它对应的值可以是任意类型的对象。从最基础的层次上看,KVC 有两个方法:一个是设置 key 的值,另一个是获取 key 的值。如下面的例子:
void changeName(Person *p, NSString *newName)
{
// using the KVC accessor (getter) method
NSString *originalName = [p valueForKey:@"name"];
// using the KVC accessor (setter) method.
[p setValue:newName forKey:@"name"];
NSLog(@"Changed %@'s name to: %@", originalName, newName);
}
?
现在,如果 Person 有另外一个 key 配偶(spouse),spouse 的 key 值是另一个 Person 对象,用 KVC 可以这样写:
voidlogMarriage(Person *p)
{
// just using the accessor again, same as example above
NSString *personsName = [p valueForKey:@"name"];
// this line is different, because it is using
// a "key path" instead of a normal "key"
NSString *spousesName = [p valueForKeyPath:@"spouse.name"];
NSLog(@"%@ is happily married to %@", personsName, spousesName);
}
?
key 与 key pat 要区分开来,key 可以从一个对象中获取值,而 key path 可以将多个 key 用点号 “.” 分割连接起来,比如:
[p valueForKeyPath:@"spouse.name"];
相当于这样……
[[p valueForKey:@"spouse"] valueForKey:@"name"];
2 KVO(Key-Value Observing)
Key-Value Observing (KVO) 建立在 KVC 之上,它能够观察一个对象的 KVC key path 值的变化。举个例子,用代码观察一个 person 对象的 address 变化,以下是实现的三个方法:
watchPersonForChangeOfAddress: 实现观察
observeValueForKeyPath:ofObject:change:context: 在被观察的 key path 的值变化时调用。
dealloc 停止观察
static NSString *const KVO_CONTEXT_ADDRESS_CHANGED = @"KVO_CONTEXT_ADDRESS_CHANGED"
@implementation PersonWatcher
-(void) watchPersonForChangeOfAddress:(Person *)p
{
// this begins the observing
[p addObserver:self
forKeyPath:@"address"
options:0
context:KVO_CONTEXT_ADDRESS_CHANGED];
// keep a record of all the people being observed,
// because we need to stop observing them in dealloc
[m_observedPeople addObject:p];
}
// whenever an observed key path changes, this method will be called
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
// use the context to make sure this is a change in the address,
// because we may also be observing other things
if(context == KVO_CONTEXT_ADDRESS_CHANGED) {
NSString *name = [object valueForKey:@"name"];
NSString *address = [object valueForKey:@"address"];
NSLog(@"%@ has a new address: %@", name, address);
}
}
-(void) dealloc;
{
// must stop observing everything before this object is
// deallocated, otherwise it will cause crashes
for(Person *p in m_observedPeople){
[p removeObserver:self forKeyPath:@"address"];
}
[m_observedPeople release];
m_observedPeople = nil;
[super dealloc];
}
-(id) init;
{
if(self = [super init]){
m_observedPeople = [NSMutableArray new];
}
return self;
}
@end
这就是 KVO 的作用,它通过 key path 观察对象的值,当值发生变化的时候会收到通知。
相关文章推荐
- IOS最新好用的框架推荐 2014-10-29
- iOS开发篇(九)FMDB的使用与介绍2
- iOS 全局变量
- 提高iOS开发效率的第三方框架(更新中)
- 七款Debug工具推荐:iOS开发必备的调试利器
- ios学习--属性字符串NSAttributeString与NSString相互转换包含图片和emoji图
- 《精通iOS开发·第六版》数据持久化基础知识之SOLite3持久化
- 《精通iOS开发·第六版》数据持久化基础知识之CoreData持久化
- 《精通iOS开发·第六版》数据持久化基础知识之属性列表持久化
- iOS开发遇到的问题
- iOS应用性能调优的25个建议和技巧
- 《精通iOS开发·第六版》数据持久化基础知识之对象归档持久化
- iOS navigationController push的时候全屏显示下一个页面
- [ios]当前线程暂时休眠
- ios文件管理
- ios文件读取
- IOS 中的 AppDelegate.m/h
- iOS证书、签名详解
- iOS-label出现未知边框线的bug
- 适合新人学习的iOS官方Demo