KVO俗称“键值监听”,可以用于监听某个对象属性值的改变
一、KVO监听属性值改变
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@property (strong, nonatomic) Person *person1;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.person1 = [[Person alloc] init];
self.person1.age = 1;
// 给person1对象添加KVO监听
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
[self.person1 addObserver:self forKeyPath:@"age" options:options context:nil];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.person1.age = 20;
}
// 当监听对象的属性值发生改变时,就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
NSLog(@"监听到%@的%@属性值改变了 - %@ - %@", object, keyPath, change, context);
}
- (void)dealloc {
// 移除KVO
[self.person1 removeObserver:self forKeyPath:@"age"];
}
@end
二、KVO实现属性监听的原理
1、NSKVONotifying_Person类
image01.png从上图控制台打印可知未使用KVO监听的对象person2的isa指向的是它自己的类对象
使用KVO监听的对象person1的isa指向的是NSKVONotifying_Person类对象
NSKVONotifying_Person是runtimeAPI动态生成的一个类,是Person的子类
2、KVO监听属性值改变的过程
在Person.m中重写willChangeValueForKey和didChangeValueForKey
#import "Person.h"
@implementation Person
- (void)setAge:(int)age {
_age = age;
NSLog(@"setAge:");
}
- (void)willChangeValueForKey:(NSString *)key {
[super willChangeValueForKey:key];
NSLog(@"willChangeValueForKey");
}
- (void)didChangeValueForKey:(NSString *)key {
NSLog(@"didChangeValueForKey - begin");
[super didChangeValueForKey:key];
NSLog(@"didChangeValueForKey - end");
}
@end
image02.png
从上图控制台打印可知当修改实例对象的属性时,会调用Foundation的_NSSetIntValueAndNotify函数
_NSSetIntValueAndNotify函数内部会调用:
1、willChangeValueForKey:
2、父类原来的setter实现
3、didChangeValueForKey:
4、didChangeValueForKey内部会触发监听器(Oberser)的监听方法(observeValueForKeyPath: ofObject: change: context:)
三、NSKVONotifying_Person类对象
#import "ViewController.h"
#import "Person.h"
#import <objc/runtime.h>
@interface ViewController ()
@property (strong, nonatomic) Person *person1;
@property (strong, nonatomic) Person *person2;
@end
@implementation ViewController
- (void)printMethodNamesOfClass:(Class)cls {
unsigned int count;
// 获得方法数组
Method *methodList = class_copyMethodList(cls, &count);
// 存储方法名
NSMutableString *methodNames = [NSMutableString string];
// 遍历所有的方法
for (int i = 0; i < count; i++) {
// 获得方法
Method method = methodList[I];
// 获得方法名
NSString *methodName = NSStringFromSelector(method_getName(method));
// 拼接方法名
[methodNames appendString:methodName];
[methodNames appendString:@", "];
}
// 释放
free(methodList);
// 打印方法名
NSLog(@"%@ %@", cls, methodNames);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.person1 = [[Person alloc] init];
self.person1.age = 1;
self.person2 = [[Person alloc] init];
self.person2.age = 2;
// 给person1对象添加KVO监听
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
[self.person1 addObserver:self forKeyPath:@"age" options:options context:@"123"];
[self printMethodNamesOfClass:object_getClass(self.person1)];
[self printMethodNamesOfClass:object_getClass(self.person2)];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.person1 willChangeValueForKey:@"age"];
[self.person1 didChangeValueForKey:@"age"];
}
// observeValueForKeyPath:ofObject:change:context:
// 当监听对象的属性值发生改变时,就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
NSLog(@"监听到%@的%@属性值改变了 - %@ - %@", object, keyPath, change, context);
}
- (void)dealloc {
[self.person1 removeObserver:self forKeyPath:@"age"];
}
@end
image03.png
从上图控制台打印可知添加了KVO监听动态生成的NSKVONotifying_Person类对象中存储的信息主要包括:isa指针、superclass指针、setAge:、class、dealloc、_isKVOA
未添加KVO监听的Person类对象中存储的信息主要包括:isa指针、superclass指针、setAge:、age
四、如何手动触发KVO?
手动调用 willChangeValueForKey: 和 didChangeValueForKey: 即可触发KVO
五、直接修改成员变量会触发KVO吗?
直接修改成员变量不会触发KVO,KVO的本质是替换了setter方法的实现,所以只有通过set方法修改才会触发KVO
六、通过KVC修改属性会触发KVO吗?
通过KVC修改属性会触发KVO
KVC通过setValue:forKey:直接修改成员变量的值的话会调用willChangeValueForKey:和didChangeValueForKey:从而触发KVO
七、KVC的赋值和取值过程是怎样的?原理是什么?
KVC俗称“键值编码”,可以通过一个key来访问某个属性
// 常见的API
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath;
- (void)setValue:(id)value forKey:(NSString *)key;
- (void)valueForKeyPath:(NSString *)keyPath;
- (void)valueForKey:(NSString *)key;
1、KVC赋值过程:
先调用相关方法,先后顺序是:
setter方法:setKey -> _setKey -> setIsKey
如果没有相关方法,看 + (BOOL)accessInstanceVariablesDirectly 的返回值
返回值为YES时:找成员变量,先后顺序:_key -> _isKey -> key -> isKey
返回值为NO时:以上找不到,抛异常,setValueForUndefinedKey
2、KVC取值过程:
先调用相关方法,先后顺序是:
getter方法:getKey -> key -> isKey -> _key
如果没有相关方法,看 + (BOOL)accessInstanceVariablesDirectly 的返回值
返回值为YES时:找成员变量,先后顺序:_key -> _isKey -> key -> isKey
返回值为NO时:以上找不到,抛异常,valueForUndefinedKey
网友评论