原理:动态生成子类,重写set方法,分发observeValueForKeyPath消息
自定义KVO用到的一些runtime接口
//动态生成一个root类或者某类的派生类
objc_allocateClassPair(Class superclass, const char *name,
size_t extraBytes)
//注册类
void objc_registerClassPair(Class cls)
//给类添加一个方法
BOOL class_addMethod(Class cls, SEL name, IMP imp,
const char *types)
//修改对象的指针
Class object_setClass(id obj, Class cls)
//添加关联属性
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
自定义KVO例子
#import "NSObject+WHJKVO.h"
#import <objc/message.h>
@implementation NSObject (WHJKVO)
- (void)whj_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context {
// 创建类
NSString * superClass = NSStringFromClass([self class]);
NSString *subClassName = [NSString stringWithFormat:@"whj_%@",superClass];
const char *name = [subClassName UTF8String];
Class myClass = objc_allocateClassPair([self class], name, 0);
// 注册类
objc_registerClassPair(myClass);
// 添加方法
NSString *methodName = [NSString stringWithFormat:@"set%@:",[keyPath capitalizedString]];
objc_setAssociatedObject(self, @"methodName", methodName, OBJC_ASSOCIATION_RETAIN_NONATOMIC); //Person 添加关联属性
class_addMethod(myClass, NSSelectorFromString(methodName), (IMP)myImp, "V@:@");
//修改指针
object_setClass(self, myClass);//执行这一行后self,就指向 whj_Person了
// 添加关联对象,目的保存observer,以备给observer发送消息
objc_setAssociatedObject(self, @"objc", observer, OBJC_ASSOCIATION_ASSIGN); //whj_Person 添加关联属性
}
void myImp(id self, SEL _cmd,id param) {
//调用super 的实现
//先保存子类
id class = [self class];
//指针改到父类
object_setClass(self,class_getSuperclass(class));
NSString *methodName = objc_getAssociatedObject(self, @"methodName"); // Person 获取关联属性
//调用父类的方法
objc_msgSend(self,NSSelectorFromString(methodName),param);
// 发消息
id observer = objc_getAssociatedObject(self, @"objc"); // Person 获取关联属性
objc_msgSend(observer, @selector(observeValueForKeyPath:ofObject:change:context:),param,self,nil,nil);
//指针改回来 ,这样下次属性变化还回来whj_Person类的myImp方法
object_setClass(self, class);
}