美文网首页
iOS开发-4.KVO

iOS开发-4.KVO

作者: iOS_ZZ | 来源:发表于2022-02-17 22:18 被阅读0次
    • 1.什么是KVO?
    a) KVO的全称是Key-Value-Observing,俗称"键值监听",可以用于监听某个对象属性值的改变
    
    • 2.未使用KVO监听的对象

    • 3.使用了KVO监听的对象

    • 4.查看_NSSet*AndNotify的存在

    • 5._NSSet*ValueAndNotify的内部实现

    a) 调用willChangeValueForKey:
    
    b) 调用原来的setter实现
    
    c) 调用didChangeValueForKey:
    didChangeValueForKey:内部会调用observer的observeValueForKeyPath:ofObject:change:context:方法
    
    • 6.iOS用什么方式实现对一个对象的KVO?(KVO的本质是什么?)
    a) 利用RuntimeAPI动态生成一个子类NSKVONotifying_类名继承自类名的类,并且让instance对象的isa指向这个全新的子类
    
    b) 当修改instance对象的属性时,会调用Foundation的_NSSetXXXValueAndNotify函数
        1) willChangeValueForKey:
        
        2) 父类原来的setter
        
        3) didChangeValueForKey:
            3.1) 内部会触发监听器(Oberser)的监听方法(observeValueForKeyPath:ofObject:change:context:)
    
    • 7.如何手动触发KVO
    a) 手动调用willChangeValueForKey:和didChangeValueForKey:
    
    • 8.直接修改成员变量会触发KVO么?
    a) 不会触发KVO 解读KVO的本质即可即实则修改了属性setter方法的实现
    
    • 9.KVO的效率高还是代理的效率高?
    a) 肯定是代理的效率高,不需要动态生成一些方法直接拿到代理调用对应的方法即可
    
    • 10.如何验证确实执行的是_NSSet*AndNotify函数?
    @interface ViewController ()
    @property (strong, nonatomic) MJPerson *person1;
    @property (strong, nonatomic) MJPerson *person2;
    @end
    
    // 反编译工具 - Hopper
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.person1 = [[MJPerson alloc] init];
        self.person1.age = 1;
        
        self.person2 = [[MJPerson alloc] init];
        self.person2.age = 2;
        
    //    NSLog(@"person1添加KVO监听之前 - %@ %@",
    //          object_getClass(self.person1),
    //          object_getClass(self.person2));
    //    NSLog(@"person1添加KVO监听之前 - %p %p",
    //          [self.person1 methodForSelector:@selector(setAge:)],
    //          [self.person2 methodForSelector:@selector(setAge:)]);
        
        // 给person1对象添加KVO监听
        NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
        [self.person1 addObserver:self forKeyPath:@"age" options:options context:@"123"];
        
    //    NSLog(@"person1添加KVO监听之后 - %@ %@",
    //          object_getClass(self.person1),
    //          object_getClass(self.person2));
    //    NSLog(@"person1添加KVO监听之后 - %p %p",
    //          [self.person1 methodForSelector:@selector(setAge:)],
    //          [self.person2 methodForSelector:@selector(setAge:)]);
    //
    //
    //    NSLog(@"类对象 - %@ %@",
    //          object_getClass(self.person1),  // self.person1.isa
    //          object_getClass(self.person2)); // self.person2.isa
    //
    //    NSLog(@"元类对象 - %@ %@",
    //          object_getClass(object_getClass(self.person1)), // self.person1.isa.isa
    //          object_getClass(object_getClass(self.person2))); // self.person2.isa.isa 
    }
    
    • 11.如何验证NSKVONotifying_类名其他的方法?
    @interface ViewController ()
    @property (strong, nonatomic) MJPerson *person1;
    @property (strong, nonatomic) MJPerson *person2;
    @end
    
    //@implementation NSObject
    //
    //- (Class)class
    //{
    //    return object_getClass(self);
    //}
    //
    //@end
    
    // 反编译工具 - Hopper
    
    @implementation ViewController
    
    - (void)printMethodNamesOfClass:(Class)cls
    {
        unsigned int count;
        // 获得方法数组
        Method *methodList = class_copyMethodList(cls, &count);
        
        // 存储方法名
        NSMutableString *methodNames = [NSMutableString string];
        
        // 遍历所有的方法
        for (int i = 0; i < count; i++) {
            // 获得方法
            Method method = methodList[I];
            // 获得方法名
            NSString *methodName = NSStringFromSelector(method_getName(method));
            // 拼接方法名
            [methodNames appendString:methodName];
            [methodNames appendString:@", "];
        }
        
        // 释放
        free(methodList);
        
        // 打印方法名
        NSLog(@"%@ %@", cls, methodNames);
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.person1 = [[MJPerson alloc] init];
        self.person1.age = 1;
        
        self.person2 = [[MJPerson alloc] init];
        self.person2.age = 2;
        
        // 给person1对象添加KVO监听
        NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
        [self.person1 addObserver:self forKeyPath:@"age" options:options context:@"123"];
        
        [self printMethodNamesOfClass:object_getClass(self.person1)];
        [self printMethodNamesOfClass:object_getClass(self.person2)];
    }
    

    相关文章

      网友评论

          本文标题:iOS开发-4.KVO

          本文链接:https://www.haomeiwen.com/subject/hbxclrtx.html