KVO底层实现原理

添加观察者方法实现原理

#import "NSObject+KVO.h"
#import "XMKVONotifying_Person.h"
#import <objc/runtime.h>

@implementation NSObject (KVO)

-(void)XM_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:      (NSKeyValueObservingOptions)options context:(void *)context
{
 //1.动态创建NSKVONotifying_Person,NSKVONotifying_Person是Person子类,做KVO
 //2.修改当前对象的isa指针->NSKVONotifying_Person
 //3.只要调用对象的set,就会调用NSKVONotifying_Person的set方法
 //4.重写NSKVONotifying_Person的set方法,1.[super set:] 2.通知观察者,告诉你属性改变

 //修改isa,本质就是改变当前对象的类名
object_setClass(self, [XMKVONotifying_Person class]);

把观察者保存到当前对象里
// 添加关联
// id object:给哪个对象添加关联属性
// key:属性名
// value:关联值
objc_setAssociatedObject(self, @"key", observer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


#import "XMKVONotifying_Person.h"
#import <objc/runtime.h>

@implementation XMKVONotifying_Person

监听方法自动调用实现原理

-(void)setAge:(NSInteger)age
{
[super setAge:age];

// 通知观察者,属性改变
id observer = objc_getAssociatedObject(self, @"key");

[observer observeValueForKeyPath:@"age" ofObject:self change:nil context:nil];
}

推荐阅读更多精彩内容