KVO与响应式编程

响应式编程

响应式编程是一种面向数据流和变化传播的编程范式。简单来说,就是变化的结果可以自动的通过数据流传播。以a = b这个表达式为例,表示的意思是将b的值赋值给a,如果再次修改了b的值,a的值并不会自动修改。但是在响应式编程框架中,可以将a和b进行某种数据流上的绑定,改变了b的值,就会自动修改a的值,就像excel表格中的求和计算公式一样,改变一个单元格数据,就会自动改变总和。

KVO

KVO的大致实现逻辑是这样的,当观察某对象 A 时,KVO 机制动态创建一个对象A当前类的子类,并为这个新的子类重写了被观察属性 keyPathsetter 方法。setter 方法随后负责通知观察对象属性的改变状况。
Person类为例,具体逻辑如下:
1.动态创建NSKVONotifying_Person,NSKVONotifying_Person是Person子类
2.修改当前对象的isa指针指向NSKVONotifying_Person
3.只要调用对象的set,就会调用NSKVONotifying_Personset方法
4.重写NSKVONotifying_Personset方法,在重写的set方法里,主要做了两件事情,1.[super set:] 2.通知观察者,告诉你属性改变

自定义一下KVO的具体实现:

@interface NSObject (hz_KVO)

- (void)hz_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;
@end
@implementation NSObject (hz_KVO)
- (void)hz_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context{
    //动态生成一个类
    NSString *oldClassName = NSStringFromClass([self class]);
    NSString *newName = [@"NShz_" stringByAppendingString:oldClassName];
    const char * newClassName = [newName UTF8String];
    //定义  创建一个类的class
    Class hzClass = objc_allocateClassPair([self class], newClassName, 0);
    class_addMethod(hzClass, @selector(setName:), (IMP)setName, "v@:@");
    //注册
    objc_registerClassPair(hzClass);
    //指向 修改isa,本质就是改变当前对象的类名
    object_setClass(self, hzClass);
    //绑定 保存观察者对象
    objc_setAssociatedObject(self, (__bridge const void *)@"123", observer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    
}
void setName(id self,SEL _cmd,NSString * newName){
   
    //保存子类类型
    id class = [self class];
    //改变self 的isa指针
    object_setClass(self, class_getSuperclass([self class]));
    //调用父类的set方法
    objc_msgSend(self, @selector(setName:),newName);
    //拿到观察者
    id observer = objc_getAssociatedObject(self, (__bridge const void *)@"123");
    //通知观察者
    objc_msgSend(observer, @selector(observeValueForKeyPath:ofObject:change:context:),self,@"name",@{@"new":newName},nil);
    //改回子类类型
    object_setClass(self, class);
    
}

调用:

@interface ViewController ()
@property(nonatomic,strong)Dog * dog;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    Dog * dog = [[Dog alloc]init];
    [dog hz_addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
    _dog = dog;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
     NSLog(@"%@===>%@",change,_dog.name);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    static int i = 0;
    i++;
    _dog.name = [NSString stringWithFormat:@"%d",i];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

你可能感兴趣的:(KVO与响应式编程)