OC-KVC的本质及原理

面试题 - KVC

通过KVC修改属性会触发KVO么?

KVC的赋值和取值过程是怎样的?原理是什么?

KVC的基本使用

image-20210417151743134

KVC的全称是Key-Value Coding,俗称“键值编码”,可以通过一个key来访问某个属性

常见的API有:

- (void)setValue:(id)value forKeyPath:(NSString *)keyPath;
- (void)setValue:(id)value forKey:(NSString *)key;
- (id)valueForKeyPath:(NSString *)keyPath;
- (id)valueForKey:(NSString *)key;

KVC的基本使用:

@interface MJCat : NSObject
@property (assign, nonatomic) int weight;
@end

@interface MJPerson : NSObject
@property (assign, nonatomic) int age;

@property (retain, nonatomic) MJCat *cat;
@end
@implementation MJPerson
@end
@implementation MJCat
@end
      person.age = 10;
        
        NSLog(@"%@", [person valueForKey:@"age"]);
        NSLog(@"%@", [person valueForKeyPath:@"cat.weight"]);
        
        
        NSLog(@"%d", person.age);
        
        [person setValue:[NSNumber numberWithInt:10] forKey:@"age"];
        [person setValue:@30 forKey:@"age"];
        person.cat = [[MJCat alloc] init];
        [person setValue:@20 forKeyPath:@"cat.weight"];
        
        NSLog(@"%d", person.age);
        NSLog(@"%d", person.cat.weight);

RUN>

=================打印结果=================
2021-04-17 15:29:31.379302+0800 Interview01-KVC[2187:88935] 10
2021-04-17 15:29:31.379738+0800 Interview01-KVC[2187:88935] (null)
2021-04-17 15:29:31.379774+0800 Interview01-KVC[2187:88935] 10
2021-04-17 15:29:31.380053+0800 Interview01-KVC[2187:88935] 30
2021-04-17 15:29:31.380093+0800 Interview01-KVC[2187:88935] 20

通过KVC给属性赋值能触发KVO吗?

能不能试一下就知道了:

我们创建一个MJPerson类,添加一个age属性,然后给这个age属性添加一个KVO,使用KVC修改age的值:

#import "MJObserver.h"

@implementation MJObserver

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"observeValueForKeyPath - %@", change);
}

@end
MJObserver *observer = [[MJObserver alloc] init];
MJPerson *person = [[MJPerson alloc] init];
// 添加KVO监听
[person addObserver:observer forKeyPath:@"age" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
[person setValue:@10 forKey:@"age"];

RUN>

=================打印结果=================
2021-04-17 15:34:55.609944+0800 Interview01-KVC[2235:92111] observeValueForKeyPath - {
    kind = 1;
    new = 10;
    old = 0;
}

可以看到KVC访问属性的时候,的确调用了setAge:方法.那么setVlaue:forKey:是如何设值的呢?

通过KVC修改age属性会触发KVO。为什么呢?先往下看

接下来看看setValue:forKey:设值原理和valueForKey:取值原理。

KVC设值原理

KVC赋值流程

执行步骤:
1:setVlaue:forKey:首先查找setKey:方法,如果有就执行;如果没有再去查找_setKey:方法.
2:如果没有找到_setKey:方法,就去查看accessInstanceVariablesDirectly方法的返回值,这个方法的意思是:是否允许直接进入对象的成员变量?,如果返回YES,就按照_key,_isKey,key,isKey的顺序查找成员变量,如果找到直接赋值;如果没找到就抛出异常NSUnknownKeyException.
3:如果accessInstanceVariablesDirectly方法直接返回NO(默认返回YES),就直接抛出异常NSUnknownKeyException.

设值原理解释:

1.寻找setAge方法
- (void)setAge:(int)age
{
    NSLog(@"setAge: - %d", age);
}

2.寻找_setAge方法
- (void)_setAge:(int)age
{
    NSLog(@"_setAge: - %d", age);
}

3.找不到上面两个方法就调用accessInstanceVariablesDirectly问问能不能直接访问成员变量
 默认的返回值就是YES
+ (BOOL)accessInstanceVariablesDirectly
{
    return YES;
}

4.1 如果返回NO,就调用setValue:forUndefinedKey:并抛出异常NSUnknownKeyException
4.2如果返回YES,会按顺序_key,_isKey,key,isKey赋值,如果四个都找不到就报上面的错

上面我们知道KVC设值会触发KVO,但是如果没有set方法呢?就算没有set方法只有成员变量,通过KVC进行赋值也会触发KVO,可以理解它们是配套使用的。

我们先证明第一点(执行步骤1),把MJPerson类中属性删掉,然后重写setAge:,_setAge:方法:

@implementation MJPerson

- (void)setAge:(int)age{
    NSLog(@"setAge方法被调用");
}

- (void)_setAge:(int)age{
    NSLog(@"_setAge被调用");
}

@end
MJPerson *person = [[MJPerson alloc] init];
// 通过KVC修改age属性
[person setValue:@10 forKey:@"age"];

RUN>

=================打印结果=================
2021-04-17 15:42:35.598146+0800 Interview01-KVC[2348:98038] setAge方法被调用

我们把setAge:方法注释掉,再运行:

=================打印结果=================
2021-04-17 15:45:15.169572+0800 Interview01-KVC[2404:100780] _setAge被调用

通过运行结果可以很清楚的看到,[person setValue:@10 forKey:@"age"];会先去查找setAge:方法,如果有就调用,如果没有再去查找_setAge:方法.

接下来验证第二点(执行步骤2),我们在MJPerson注释掉刚才的setAge:方法,然后重写accessInstanceVariablesDirectly方法,然后返回NO,运行下看看效果:

@implementation MJPerson

+ (BOOL)accessInstanceVariablesDirectly{
    return NO;
}

@end
MJPerson *person = [[MJPerson alloc] init];
// 通过KVC修改age属性
[person setValue:@10 forKey:@"age"];

RUN>

=================打印结果=================
2021-04-17 15:48:20.837291+0800 Interview01-KVC[2437:102949] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key age.'
image-20210417154937044

再修改为return YES;然后添加如下成员变量:

@interface MJPerson : NSObject
{
@public
    int _age;
    int _isAge;
    int age;
    int isAge;
}

@end
@implementation MJPerson

+ (BOOL)accessInstanceVariablesDirectly{
    return YES;
}

@end
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        MJPerson *person = [[MJPerson alloc] init];
        // 通过KVC修改age属性
        [person setValue:@10 forKey:@"age"];              
        NSLog(@"%@", [person valueForKey:@"age"]);
    }
    return 0;
}

RUN>

=================打印结果=================
2021-04-17 16:05:21.917933+0800 Interview01-KVC[2574:111457] 10
image-20210417160635084
image-20210417160710325
image-20210417160805503

有人可能会以为是成员变量顺序导致的,我们把成员变量的顺序打乱在执行看看:

image-20210417160931763

我们把4个属性全注释掉,运行一下看看效果:

@interface MJPerson : NSObject
{
}

RUN>

=================打印结果=================
2021-04-17 16:10:24.189710+0800 Interview01-KVC[2648:117035] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key age.'

如果找到直接赋值;如果没找到就抛出异常NSUnknownKeyException.

如果用KVC修改一个类的成员变量就会触发KVO.我们来验证一下

@interface MJPerson : NSObject
{
@public
    int age;
}

@end
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        MJObserver *observer = [[MJObserver alloc] init];
        MJPerson *person = [[MJPerson alloc] init];
        
        // 添加KVO监听
        [person addObserver:observer forKeyPath:@"age" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
        
        // 通过KVC修改age属性
        [person setValue:@10 forKey:@"age"];
    }
    return 0;
}

RUN>

=================打印结果=================
2021-04-17 16:15:52.595089+0800 Interview01-KVC[2690:120312] observeValueForKeyPath - {
    kind = 1;
    new = 10;
    old = 0;
}

其实KVC内部调用了下面方法才会触发KVO的

[person willChangeValueForKey:@"age"];
person->_age = 10;
[person didChangeValueForKey:@"age"];

因为KVC赋值同样会调用willChangeValueForKey:,didChangeValueForKey:两个方法,我们在MJPerson中重写这两个方法:

- (void)willChangeValueForKey:(NSString *)key
{
    [super willChangeValueForKey:key];
    NSLog(@"willChangeValueForKey - %@", key);
}

- (void)didChangeValueForKey:(NSString *)key
{
    NSLog(@"didChangeValueForKey - begin - %@", key);
    [super didChangeValueForKey:key];
    NSLog(@"didChangeValueForKey - end - %@", key);
}

RUN>

=================打印结果=================
2021-04-17 16:18:17.974684+0800 Interview01-KVC[2707:121945] willChangeValueForKey - age
2021-04-17 16:18:17.975060+0800 Interview01-KVC[2707:121945] didChangeValueForKey - begin - age
2021-04-17 16:18:17.975254+0800 Interview01-KVC[2707:121945] observeValueForKeyPath - {
    kind = 1;
    new = 10;
    old = 0;
}
2021-04-17 16:18:17.975309+0800 Interview01-KVC[2707:121945] didChangeValueForKey - end - age

可以看到,这两个方法的确都被调用了,所以KVC给一个类的属性赋值也能触发KVO.

KVC取值原理

取值原理.png

KVC取值原理解释

1.getAge
- (int)getAge
{
    return 11;
}

2.age
- (int)age
{
    return 12;
}

3.isAge
- (int)isAge
{
    return 13;
}

4._age
- (int)_age
{
    return 14;
}

5.3.找不到上面四个方法就调用accessInstanceVariablesDirectly问问能不能直接访问成员变量
 默认的返回值就是YES
+ (BOOL)accessInstanceVariablesDirectly
{
    return YES;
}

5.1 如果返回NO,就调用valueforUndefinedKey:并抛出异常NSUnknownKeyException
5.2如果返回YES,会按顺序_key,_isKey,key,isKey赋值,如果四个都找不到就报上面的错
@implementation MJPerson

- (int)getAge
{
    return 11;
}

- (int)age
{
    return 12;
}

- (int)isAge
{
    return 13;
}

- (int)_age
{
    return 14;
}

@end
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        MJPerson *person = [[MJPerson alloc] init];
        [person setValue:@18 forKey:@"age"];
        
        NSLog(@"%@", [person valueForKey:@"age"]);
    }
    return 0;
}

Run>

2021-04-17 16:21:41.302038+0800 Interview01-KVC[2757:124317] 11
image-20210417162323944
image-20210417162342102

找不到上面四个方法就调用accessInstanceVariablesDirectly

image-20210417162508473
@implementation MJPerson

- (instancetype)init
{
    self = [super init];
    if (self) {
        _age=11;
        _isAge=21;
        age=31;
        isAge = 41;
    }
    return self;
}

// 默认的返回值就是YES
+ (BOOL)accessInstanceVariablesDirectly
{
    return YES;
}

@end
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        MJPerson *person = [[MJPerson alloc] init];
 
        
        NSLog(@"%@", [person valueForKey:@"age"]);
    }
    return 0;
}

RUN>

=================打印结果=================
2021-04-17 16:32:46.330006+0800 Interview01-KVC[2902:132396] 11

面试题 - KVC

通过KVC修改属性会触发KVO么?

会触发KVO.

KVC的赋值和取值过程是怎样的?原理是什么?

看 KVC设值原理 KVC取值原理 步骤,两张图的流程

特别备注

本系列文章总结自MJ老师在腾讯课堂iOS底层原理班(下)/OC对象/关联对象/多线程/内存管理/性能优化,相关图片素材均取自课程中的课件。如有侵权,请联系我删除,谢谢!

你可能感兴趣的:(OC-KVC的本质及原理)