OC 底层原理笔记

4OC使用原理 - 4 - KVO原理详解

2020-01-20  本文已影响0人  zysmoon
面试题
1. iOS用什么方式实现对一个对象的KVO?(KVO的本质是什么?)
2. 如何手动触发KVO?

代码佐证

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.person1 willChangeValueForKey:@"age"];
    [self.person1 didChangeValueForKey:@"age"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    NSLog(@"监听到%@的%@属性值改变了 - %@ - %@", object, keyPath, change, context);
}

运行结果

3. 直接修改成员变量会触发KVO么?

代码佐证

@interface Person : NSObject {
@public int _age;
}
@property(nonatomic,assign)int age;
@end

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    NSLog(@"监听到%@的%@属性值改变了 - %@ - %@", object, keyPath, change, context);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 直接修改成员变量会触发KVO么
    self.person1.age = 10;
}

运行结果

1653926-6ebfa0947a53f4f5.png
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"调用带下划线的成员变量");
    self.person1->_age = 10;
}

运行结果

1653926-3f8197d1a10f1950.png

可知,直接调用带下下划线的成员变量不会触发KVO,因为没有调用set方法

序言

KVO的全称是Key-Value Observing,俗称“键值监听”,可以用于监听某个对象属性值的改变。

1653926-70de605d09068f3b.png

代码佐证

// 创建一个新类
@interface Person : NSObject
@property (assign, nonatomic) int age;
@end

@implementation Person
- (void)setAge:(int)age {
    _age = age;
}
@end

// 调用
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.person1 = [[Person alloc] init];
    self.person1.age = 1;

    self.person2 = [[Person alloc] init];
    self.person2.age = 2;

    // 给person1对象添加KVO监听
    NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
    [self.person1 addObserver:self forKeyPath:@"age" options:options context:@"123"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    NSLog(@"监听到%@的%@属性值改变了 - %@ - %@", object, keyPath, change, context);
}

- (void)dealloc {
    [self.person1 removeObserver:self forKeyPath:@"age"];
}

// 触摸屏幕改变值
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // NSKVONotifying_MJPerson是使用Runtime动态创建的一个类,是MJPerson的子类

    // self.person1.isa == NSKVONotifying_MJPerson
    [self.person1 setAge:21];

    // self.person2.isa = MJPerson
    [self.person2 setAge:22];
    NSLog(@"person1 age = %d,person2 age = %d",self.person1.age,self.person2.age);
}

#import "Person.h"
@interface NSKVONotifying_Person : Person
@end

#import "NSKVONotifying_Person.h"
@implementation NSKVONotifying_Person

- (void)setAge:(int)age {
    _NSSetIntValueAndNotify();
}

// 伪代码
void _NSSetIntValueAndNotify() {
    [self willChangeValueForKey:@"age"];
    [super setAge:age];
    [self didChangeValueForKey:@"age"];
}

// 伪代码
- (void)didChangeValueForKey:(NSString *)key {
    // 通知监听器,某某属性值发生了改变
    [oberser observeValueForKeyPath:key ofObject:self change:nil context:nil];
}

@end

打印结果

1653926-5b99e170cd06ca9d.png
底层分析
![1653926-567fb168151eaef4.png](https://img.haomeiwen.com/i5312796/99b7b07b7d5025ab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

底层原理解析

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.person1 = [[Person alloc] init];
    self.person1.age = 1;

    self.person2 = [[Person alloc] init];
    self.person2.age = 2;

    NSLog(@"person1添加KVO监听之前 - %@ %@",object_getClass(self.person1),object_getClass(self.person2));

    // 给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));
}

打印结果

1653926-256100b95c5631ae.png

通过打印结果,可以发现person1在添加监听之后class类型发生了变化

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.person1 = [[Person alloc] init];
    self.person1.age = 1;

    self.person2 = [[Person 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:)]);
}

打印结果

1653926-34c95759e55f0703.png

通过打印我们知道添加监听之后,调用setAge方法发生了变化

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.person1 = [[Person alloc] init];
    self.person1.age = 1;

    self.person2 = [[Person alloc] init];
    self.person2.age = 2;

    // 给person1对象添加KVO监听
    NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
    [self.person1 addObserver:self forKeyPath:@"age" options:options context:@"123"];

    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
}

运行结果

1653926-dd152f514f61e0b1.png

可以得出在添加监听之后,实例对象了类对象的isa发生了变化

_NSSet*ValueAndNotify的内部实现

1653926-9b7343300e5f15cb.png

代码例子佐证

#import "Person.h"

@implementation Person

- (void)setAge:(int)age {
    _age = age;
}

- (void)willChangeValueForKey:(NSString *)key {
    [super willChangeValueForKey:key];

    NSLog(@"willChangeValueForKey");
}

- (void)didChangeValueForKey:(NSString *)key {
    NSLog(@"didChangeValueForKey - begin");

    [super didChangeValueForKey:key];

    NSLog(@"didChangeValueForKey - end");
}

@end

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.person1 setAge:21];
}

运行结果

1653926-7fbc8d750a4e5385.png
_NSSet*ValueAndNotify的内部实现 - Class
#import "NSKVONotifying_Person.h"

@implementation NSKVONotifying_Person

- (void)setAge:(int)age {
    _NSSetIntValueAndNotify();
}

// 屏幕内部实现,隐藏了NSKVONotifying_MJPerson类的存在
- (Class)class {
    return [MJPerson class];
}

- (void)dealloc {
    // 收尾工作
}

- (BOOL)_isKVOA {
    return YES;
}
@end

- (void)viewDidLoad {
    [super viewDidLoad];

    self.person1 = [[Person alloc] init];
    self.person1.age = 1;

    self.person2 = [[Person alloc] init];
    self.person2.age = 2;

    // 给person1对象添加KVO监听
    NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
    [self.person1 addObserver:self forKeyPath:@"age" options:options context:@"123"];

    NSLog(@"类对象 - %@ %@",[self.person1 class],[self.person2 class]);
    NSLog(@"类对象 - %@ %@",object_getClass(self.person1),object_getClass(self.person2));
}

运行结果

1653926-1a32099c1cd6b4ea.png
打印 _NSSet*ValueAndNotify所有方法
// 打印class所有方法列表
- (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];
    // Do any additional setup after loading the view, typically from a nib.

    self.person1 = [[Person alloc] init];
    self.person1.age = 1;

    self.person2 = [[Person 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)];
}

运行结果


本文参考:
路飞_Luck (https://www.jianshu.com/p/07f7b96bb03f)
以及借鉴MJ的教程视频
非常感谢.


项目连接地址 - kvo-detail

上一篇 下一篇

猜你喜欢

热点阅读