1. KVO
- 全称:key-value observing,俗称:键值监听
2. KVO的本质
- 未使用KVO监听的类,其isa指向它的类对象
- 使用了KVO监听的类,其isa指向一个通过runtime生成的NSKVONotifying_ClassName类对象。


- 对NSKVONotifying_Person类对象的方法列表获取
-(void)getClassMethodList:(Class)cls{
unsigned int count;
//获取类对象的方法列表
Method * methodList = class_copyMethodList(cls, &count);
NSMutableString * methodMutableString = [NSMutableString string];
for (int i =0; i < count; i ++) {
Method method = methodList[I];
//拼接方法
NSString * methodStr = NSStringFromSelector(method_getName(method));
[methodMutableString appendString:methodStr];
[methodMutableString appendString:@"\n"];
}
free(methodList);
NSLog(@"%@",methodMutableString);
}

3.KVO调用的顺序
-(void)setName:(NSString *)name{
_name = name;
}
-(void)willChangeValueForKey:(NSString *)key{
[super willChangeValueForKey:key];
NSLog(@"%s",__func__);
}
- (void)didChangeValueForKey:(NSString *)key{
NSLog(@"%s -- begin",__func__);
[super didChangeValueForKey:key];
NSLog(@"%s -- end",__func__);
}

4.补充KVC
setValue: forKey: 的原理

valueForKey:的原理

网友评论