Apple 使用了 isa 混写(isa-swizzling)来实现 KVO 。当观察对象A时,KVO机制动态创建一个新的名为:NSKVONotifying_A 的新类,该类继承自对象A的本类,且 KVO 为 NSKVONotifying_A 重写观察属性的 setter 方法,setter 方法会负责在调用原 setter 方法之前和之后,通知所有观察对象属性值的更改情况。
自己实现KVO
由于KVO要对所有的NSObject都可以使用,所以创建NSObject的Category
NSObject+NNKVO.h
#import <Foundation/Foundation.h>
@interface NSObject (NNKVO)
- (void)NN_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context;
@end
NSObject+NNKVO.m
#import "NSObject+NNKVO.h"
#import <objc/message.h>
@implementation NSObject (NNKVO)
- (void)NN_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context {
//1. 创建子类
NSString *originClassName = NSStringFromClass(self.class);
NSString *newClassName = [@"NSKVONotifying_" stringByAppendingString:originClassName];
Class newClass = objc_allocateClassPair(self.class, newClassName.UTF8String, 0);
objc_registerClassPair(newClass);//创建子类后,需要register
//2. 创建方法,需要重写父类的setter方法
class_addMethod(newClass, @selector(setName:), (IMP)(NN_SetMethod), "");
//3. 更改类型
object_setClass(self, newClass);
[[self getObservers] setObject:observer forKey:keyPath];
}
// setter方法
void NN_SetMethod(id self, SEL method, id setValue) {
Class subClass = object_getClass(self); // 获取当前类
Class superClass = class_getSuperclass(subClass); // 获取父类
object_setClass(self, superClass); // 更改当前对象的类型
((void (*) (id, SEL, id)) objc_msgSend) (self, method, setValue); // 调用setter方法,进行数据更改
object_setClass(self, subClass); // 更改回原来的子类
// 触发回调observe更新了属性
for (id objc in [self getObservers].allValues) {
((void (*) (id, SEL, id, id, id ,id)) objc_msgSend)(objc, @selector(observeValueForKeyPath:ofObject:change:context:), @"name", self, nil, nil);
}
- (NSMutableDictionary *)getObservers {
NSMutableDictionary *observers = objc_getAssociatedObject(self, _cmd);
if (!observers) {
observers = [NSMutableDictionary dictionary];
objc_setAssociatedObject(self, _cmd, observers, OBJC_ASSOCIATION_RETAIN);
}
return observers;
}
}
@end
网友评论