自定义KVO(探索KVO底层本质)

2018-01-29  本文已影响0人  锦鲤跃龙

KVO底层实现原理

原理图

自定义KVO (NSObject+kvo)

头文件

#import <Foundation/Foundation.h>

@interface NSObject (kvo)
- (void)LYJ_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context;

- (void)LYJ_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context block:(void(^)(id object ,NSDictionary *change))block;

@end

体文件

/*
 OC的方法
 1.SEL  目录
 2.IMP  页码
 代码    内容
 
 */

#import "NSObject+kvo.h"
#import <objc/message.h>

@implementation NSObject (kvo)

//自定义的KVO
-(void)LYJ_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context
{
    //1.创建一个类
    //Person  LYJKVO_Person
    NSString * oldName = NSStringFromClass([self class]);
    NSString * newName = [@"LYJKVO_" stringByAppendingString:oldName];
    //添加类
    
    //- 1.这个类继承谁
    //- 2.类名
    Class MyClass = objc_allocateClassPair([self class], newName.UTF8String, 0);
    //注册类
    objc_registerClassPair(MyClass);
    
    
    //2.动态修改self的类型!!
    object_setClass(self, MyClass);
    
    //3.重写setName:  -- 给子类对象添加setName:方法!!
    class_addMethod(MyClass, @selector(setName:), (IMP)setName, "v@:@");
    
    //4.将观察者绑定到对象上面
    objc_setAssociatedObject(self, (__bridge const void *)@"objc", observer, OBJC_ASSOCIATION_ASSIGN);
    
    
}

-(void)LYJ_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context block:(void (^)(id, NSDictionary *))block
{
    
}

void setName(id self,SEL _cmd,NSString * newName){
    struct objc_super person = {self,class_getSuperclass([self class])};
    //修改name属性!!
    objc_msgSendSuper(&person, _cmd,newName);
    
    //拿出观察者!!
    id observer = objc_getAssociatedObject(self, @"objc");
    //调用observer的
    objc_msgSend(observer, @selector(observeValueForKeyPath:ofObject:change:context:),@"name",self,@{@"name":newName},nil);
}

调用

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    Person * p = [[Person alloc] init];
    
    //添加观察者!!
//    [p addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
    
    [p LYJ_addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
    
    
//   [p LYJ_addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil block:^(id object, NSDictionary *change) {
//       
//       
//       //实现!!
//       
//   }];
//    [p LYJ_addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil block:^(id object, NSDictionary *change) {
//       //说先!!
//    }];
//
//    [p LYJ_addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil block:^(id object, NSDictionary *change) {
//        
//    }];
//
//    [p LYJ_addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil block:^(id object, NSDictionary *change) {
//        
//    }];
//
//    [p LYJ_addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil block:^(id object, NSDictionary *change) {
//        
//    }];
//
//    [p LYJ_addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil block:^(id object, NSDictionary *change) {
//        
//    }];

    
    _p = p;
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    static int a = 0;
    a++;
    self.p.name = [NSString stringWithFormat:@"%d",a];
//    NSLog(@"%@",self.p.name);
}


@end

调用可以看到


这里写图片描述

此时person为LYJKVO_PERSON 为person的子类

上一篇下一篇

猜你喜欢

热点阅读