代码自己实现,深入探究KVO的内部实现
程序员文章站
2022-04-13 12:25:24
...
#import "NSObject+KVO.h"
#import <objc/message.h>
@implementation NSObject_KVO
- (void)WK_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context{
/*
自定义子类
重写方法
修改isa指针
*/
NSString *oldClassName = NSStringFromClass([self class]);
NSString *newClassName = [ @"WKKVO_" stringByAppendingString:oldClassName];
const char *newName = [newClassName UTF8String];
Class Mycalss = objc_allocateClassPair([self class], newName, 0);
//添加set方法。相当于重写!!
/*
子类没有这个方法就会去父类找这个方法,但是并不代表子类拥有了这个方法。
*/
class_addMethod(Mycalss, NSSelectorFromString(@"setName"), (IMP)setName, "aaa@qq.com:@");
//注册这个类
objc_registerClassPair(Mycalss);
//修改self的isa指针
object_setClass(self, Mycalss);
//将观察者保存到当前对象
objc_setAssociatedObject(self, (__bridge const void *)@"objc", observer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
void setName (id self , SEL _cmd ,NSString *newName){
//保存当前类型
id class = [self class];
//改变isa指针
object_setClass(self, class_getSuperclass(class));
//调用父类set方法
objc_msgSend(self, @selector(setName:),newName);
//拿出观察者
id objc = objc_getAssociatedObject(self, (__bridge const void *)@"objc");
//通知观察者
objc_msgSend(objc, @selector(observeValueForKeyPath:ofObject:change:context:), @"name",self,nil,nil);
//改回子类类型
object_setClass(self, class);
/*
将XCode升级到6后,报Too many arguments to function call, expected 0, have *,在XCode5.1里能编译通过的,到xcode6就报错
objc_msgSend(objc, @selector(observeValueForKeyPath:ofObject:change:context:), @"name",self,nil,nil);
Too many arguments to function call, expected 0, have *
问了下度娘,
选中项目 - Project - Build Settings - ENABLE_STRICT_OBJC_MSGSEND 将其设置为 NO 即可
*/
}
@end