iOS

iOS底层探索之KVC

2021-08-03  本文已影响0人  俊而不逊

前言

iOS的面试中,KVC几乎是必问的面试题之一,那么到底什么是KVCKVC的原理是什么呢?

核心内容

KVC又称键值编码Key-Value-Coding),在iOS开发中是一个比较常见的技术点,相信很多开发人员都使用过KVC,其主要的两个方法就是如下两个,分别对应设置值和取值:

- (void)setValue:(nullable id)value forKey:(NSString *)key;
- (nullable id)valueForKey:(NSString *)key;

1. KVC简介

我们日常的开发也是经常用到,如下:

// 访问对象属性, 我们可以直接通过setter/getter方法, 或者通过成员变量直接进行访问操作.
JPStudent *stu = [[JPStudent alloc] init];
stu.name      = @"RENO"; // setter
NSString *name   = stu.name; // getter
person->nickName = @"瑞诺"; // 直接访问成员

// kvc 间接访问
[stu setValue:@"卡卡西" forKey:@"name"];
NSString *name2 = [stu valueForKey:@"name"];

除了纯代码开发中经常使用到KVC,在Xibstoryboard中也可以使用,如下图:

storyboard设置键值

Xibstoryboard当中的使用,和代码是类似的,设置键值,键是KeyPath,还有值的的类型Type,再设置值Value

1.1 KCV官方文档

键值编码是由 NSKeyValueCoding 非正式协议启用的一种机制,对象采用该机制来提供对其属性间接访问。当对象符合键值编码时,其属性可通过字符串参数通过简洁、统一的消息传递接口进行寻址。这种间接访问机制补充了实例变量及其相关访问器方法提供的直接访问。

一种可以通过名称或键间接访问对象属性的机制,访问对象值的基本方法是,它设置由指定键标识的属性的值,和,它返回由指定键标识的属性的值。因此,可以以一致的方式访问对象的所有属性。setValue(_:forKey:)value(forKey:)

默认实现依赖于通常由对象实现的访问器方法(或者在需要时直接访问实例变量)

1.2 常用KVC 方式

其中KVC中最为基础的两个 API就是 setValue: forKey:valueForKey:,分别是根据 Key 设置值 和取出值,其用法如上面例子所示。此外,还有其他的一些使用方式。

KVC - 集合类型

例如:需要修改数组里面的值,person.array[0] = @"100"

person.array = @[@"1",@"2",@"3"];

    // 第一种:搞一个新的数组 - KVC 赋值就OK
    NSArray *array = [person valueForKey:@"array"];
    array = @[@"100",@"2",@"3"];
    [person setValue:array forKey:@"array"];
    NSLog(@"%@",[person valueForKey:@"array"]);
新数组 - KVC 赋值
// 第二种
    NSMutableArray *mArray = [person mutableArrayValueForKey:@"array"];
    mArray[0] = @"200";
    NSLog(@"%@",[person valueForKey:@"array"]);
使用mutableArrayValueForKey
/* 
   Given a key that identifies an _ordered_ to-many relationship, return a mutable 
   array that provides read-write access to the related objects. Objects added to the
   mutable array will become related to the receiver, and objects removed from the 
   mutable array will become unrelated.
*/
- (NSMutableArray *)mutableArrayValueForKey:(NSString *)key;

在对象包含一个数据变量时,如果调用该方法,会得到一个可变数组,通过修改该数组中的元素,使得原数组也发生变更,即使原数组是不可变数组。

数组 @[@"1", @"2", @"3"],是不可变数组,原本是不可以被修改,但是通过调用该方法,得到的是一个可变的数组,并修改其中的值后,发现原数组也变改变了,如上图所示。

在平时我们使用 NSArray数组的时候,用到最多的就是 objectAtIndex:,根据下标进行取值。但是在查看KVC的定义时可以发现,NSArrayNSMutableArray 也有 valueForKey的方法,如下代码所示:

**@interface** NSArray<ObjectType>(NSKeyValueCoding)
/* Return an array containing the results of invoking -valueForKey: 
on each of the receiver's elements. The returned array will contain 
NSNull elements for each instance of -valueForKey: returning nil.*/

- (**id**)valueForKey:(NSString *)key;

/* Invoke -setValue:forKey: on each of the receiver's elements.*/

- (**void**)setValue:(**nullable** **id**)value forKey:(NSString *)key;
@end

结构体类型

typedef struct {
    float x, y, z;
} ThreeFloats;

ThreeFloats floats = {1.,2.,3.};
    NSValue *value     = [NSValue valueWithBytes:&floats objCType:@encode(ThreeFloats)];
    [person setValue:value forKey:@"threeFloats"];
    NSValue *value1    = [person valueForKey:@"threeFloats"];

聚合操作符

#pragma mark - 聚合操作符
// @avg、@count、@max、@min、@sum
- (void)aggregationOperator{
    NSMutableArray *personArray = [NSMutableArray array];
    for (int i = 0; i < 6; i++) {
        JPStudent *p = [JPStudent new];
        NSDictionary* dict = @{
                               @"name":@"Tom",
                               @"age":@(18+i),
                               @"nick":@"Cat",
                               @"length":@(175 + 2*arc4random_uniform(6)),
                               };
        [p setValuesForKeysWithDictionary:dict];
        [personArray addObject:p];
    }
    NSLog(@"%@", [personArray valueForKey:@"length"]);
    
    /// 平均身高
    float avg = [[personArray valueForKeyPath:@"@avg.length"] floatValue];
    NSLog(@"%f", avg);
    
    int count = [[personArray valueForKeyPath:@"@count.length"] intValue];
    NSLog(@"%d", count);
    
    int sum = [[personArray valueForKeyPath:@"@sum.length"] intValue];
    NSLog(@"%d", sum);
    
    int max = [[personArray valueForKeyPath:@"@max.length"] intValue];
    NSLog(@"%d", max);
    
    int min = [[personArray valueForKeyPath:@"@min.length"] intValue];
    NSLog(@"%d", min);
}

字典类型

#pragma mark - 字典操作

- (void)dictionaryTest{
    
    NSDictionary* dict = @{
                           @"name":@"Cooci",
                           @"nick":@"KC",
                           @"subject":@"iOS",
                           @"age":@18,
                           @"length":@180
                           };
    JPStudent *p = [[JPStudent alloc] init];
    // 字典转模型
    [p setValuesForKeysWithDictionary:dict];
    NSLog(@"%@",p);
    // 键数组转模型到字典
    NSArray *array = @[@"name",@"age"];
    NSDictionary *dic = [p dictionaryWithValuesForKeys:array];
    NSLog(@"%@",dic);
}

2. KVC 设值 setValue:forKey

KVC的设值是怎么样的呢?在官方文档中的说明如下图:

Search Pattern for the Basic Setter

大致的意思是:

[stu setValue:@"RENO" forKey:@"name"]为例,可以得出以下结论:

优先通过setter方法,进行属性设置,调用顺序是:

如果以上方法均未找到,并且accessInstanceVariablesDirectly返回YES,则通过成员变量进行设置,顺序是:

大家可自己通过代码进行验证,这里不再进行代码演示了,这里需要重点说下accessInstanceVariablesDirectly

重写+ (BOOL)accessInstanceVariablesDirectly方法让其返回NO,这样的话,如果KVC没有找到set<Key>_set<Key>setIs<Key>相关方法时,会直接用setValue:forUndefinedKey:方法。

那么下面用代码来体验一把。

NS_ASSUME_NONNULL_BEGIN

@interface JPPerson : NSObject{
    @public
        NSString *_isName;
       NSString *name;
      NSString *isName;
    NSString *_name;

}
@end

NS_ASSUME_NONNULL_END
#pragma mark - 关闭或开启实例变量赋值
+ (BOOL)accessInstanceVariablesDirectly{
    return NO;
}
-(id)valueForUndefinedKey:(NSString *)key{
     NSLog(@"异常信息打印,该key不存在%@",key);
     return nil;
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
      NSLog(@"设值异常打印,该key不存在%@",key);
}

//MARK: - setKey. 的流程分析
//- (void)setName:(NSString *)name{
//    NSLog(@"%s - %@",__func__,name);
//}

//- (void)_setName:(NSString *)name{
//    NSLog(@"%s - %@",__func__,name);
//}

//- (void)setIsName:(NSString *)name{
//    NSLog(@"%s - %@",__func__,name);
//}

// 没有调用
//- (void)_setIsName:(NSString *)name{
//    NSLog(@"%s - %@",__func__,name);
//}
    LGPerson* person = [LGPerson new];
    [person setValue:@"NewName" forKey:@"name"];
    NSString* name = [person valueForKey:@"name"];
    NSLog(@"value for key : %@",name);

    NSLog(@"_name:%@",person->_name);
    NSLog(@"_isName:%@",person->_isName);
    NSLog(@"name:%@",person->name);
    NSLog(@"isName:%@",person->isName);
2021-08-02 13:08:46.060908+0800 002-KVC取值&赋值过程[9684:199214] 设值异常打印,该key不存在name
2021-08-02 13:08:46.061885+0800 002-KVC取值&赋值过程[9684:199214] 异常打印,该key不存在name
2021-08-02 13:08:46.062117+0800 002-KVC取值&赋值过程[9684:199214] value for key : (null)
2021-08-02 13:08:46.062344+0800 002-KVC取值&赋值过程[9684:199214] _name:(null)
2021-08-02 13:08:46.062480+0800 002-KVC取值&赋值过程[9684:199214] _isName:(null)
2021-08-02 13:08:46.062597+0800 002-KVC取值&赋值过程[9684:199214] name:(null)
2021-08-02 13:08:46.062699+0800 002-KVC取值&赋值过程[9684:199214] isName:(null)
2021-08-02 13:08:46.391636+0800 002-KVC取值&赋值过程[9684:199214] 异常打印,该key不存在name
2021-08-02 13:08:46.391807+0800 002-KVC取值&赋值过程[9684:199214] 取值:(null)

这说明了重写+(BOOL)accessInstanceVariablesDirectly方法让其返回NO后,KVC找不到set<Key>等方法后,不再去找<Key>系列成员变量,而是直接调用setValue:forUndefinedKey:方法。

3. KVC取值 valueForKey

上面分析完KVC设值,现在分析下KVC取值,当调用valueForKey:的代码时,OC底层的执行机制又是怎样的呢?官方文档的说明如下:

Search Pattern for the Basic Getter

以上翻译大概可以总结一下几个流程:

以上流程中有针对数组和集合的处理,而对于字典的处理并不在这一步骤中,字典的KVC实际上是调用自己的 setObject:forKey:objectForKey:方法,并不会查找自身的成员变量和属性。

//MARK: - valueForKey 流程分析 - get<Key>, <key>, is<Key>, or _<key>,

- (NSString *)getName{
    return NSStringFromSelector(_cmd);
}

//- (NSString *)name{
//    return NSStringFromSelector(_cmd);
//}
//
//- (NSString *)isName{
//    return NSStringFromSelector(_cmd);
//}
//
//- (NSString *)_name{
//    return NSStringFromSelector(_cmd);
//}

    person->_name = @"_name";
    person->_isName = @"_isName";
    person->name = @"name";
    person->isName = @"isName";

    NSLog(@"取值:%@",[person valueForKey:@"name"]);

以上代码依次注释,测试打印,如下

我就不一一的去测试打印截图了,其他的都是一样的,有兴趣的可以自己去测试下!

通过打印测试,对于方法的查找次序确实是如第一步 get<Key><key>is<Key>_<key>,并且不会调用其他的方法。

打印结果没有问题,通过验证发现变量的调用顺序确实如_<key>, _is<Key>, <key>, is<Key>,并且最终未找到时会抛出异常valueForUndefinedKey:(这里没有一一列举出所有的打印过程出来)

成员变量定义
成员变量赋值

当注释_name的赋值语句,但_name定义并未注释时,并没有因为_name值为null,就去查找_isName,而是直接取_name的空值null

这是因为是按顺序来的,虽然_name的赋值注释了,但是它的定义还在,就是老大还在,老二不能上位,除非老大不在。
如下图所示:

定义注释
在这里插入图片描述

4. 自定义KVC

我们也可以自己实现一个非正式协议对象采用该机制来提供对其属性间接访问,自定义KVC,如下代码:

// KVC 自定义
@implementation NSObject (LGKVC)

// 设置
- (void)lg_setValue:(nullable id)value forKey:(NSString *)key{
    // 1: 判断什么 key
    if (key == nil || key.length == 0) {
        return;
    }

    // 2: setter set<Key>: or _set<Key>,
    // key 要大写
    NSString *Key = key.capitalizedString;

    // 拼接方法
    NSString *setKey = [NSString stringWithFormat:@"set%@:",Key];
    NSString *_setKey = [NSString stringWithFormat:@"_set%@:",Key];
    NSString *setIsKey = [NSString stringWithFormat:@"setIs%@:",Key];

    // 是否存在方法
    if ([self lg_performSelectorWithMethodName:setKey value:value]) {
        NSLog(@"*********%@**********",setKey);
        return;
    }else if ([self lg_performSelectorWithMethodName:_setKey value:value]) {
        NSLog(@"*********%@**********",_setKey);
        return;
    }else if ([self lg_performSelectorWithMethodName:setIsKey value:value]) {
        NSLog(@"*********%@**********",setIsKey);
        return;
    }

    // 3: 判断是否响应 accessInstanceVariablesDirectly 返回YES NO 奔溃
    // 3:判断是否能够直接赋值实例变量——NO
    if (![self.class accessInstanceVariablesDirectly] ) {
        @throw [NSException exceptionWithName:@"LGUnknownKeyException" reason:[NSString stringWithFormat:@"****[%@ valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.****",self] userInfo:nil];

    }

    // 4: 间接变量
    // 获取 ivar -> 遍历 containsObjct -
    // 4.1 定义一个收集实例变量的可变数组
    NSMutableArray *mArray = [self getIvarListName];
    // _<key> _is<Key> <key> is<Key>
    // 拼接成员变量
    NSString *_key = [NSString stringWithFormat:@"_%@",key];
    NSString *_isKey = [NSString stringWithFormat:@"_is%@",Key];
    NSString *isKey = [NSString stringWithFormat:@"is%@",Key];

    // 是否存在对应的变量
    if ([mArray containsObject:_key]) {
        // 4.2 获取相应的 ivar
       Ivar ivar = class_getInstanceVariable([self class], _key.UTF8String);
        // 4.3 对相应的 ivar 设置值
       object_setIvar(self , ivar, value);
       return;
    }else if ([mArray containsObject:_isKey]) {
       Ivar ivar = class_getInstanceVariable([self class], _isKey.UTF8String);
       object_setIvar(self , ivar, value);
       return;
    }else if ([mArray containsObject:key]) {
       Ivar ivar = class_getInstanceVariable([self class], key.UTF8String);
       object_setIvar(self , ivar, value);
       return;
    }else if ([mArray containsObject:isKey]) {
       Ivar ivar = class_getInstanceVariable([self class], isKey.UTF8String);
       object_setIvar(self , ivar, value);
       return;
    }

    // 5:如果找不到相关实例

    @throw [NSException exceptionWithName:@"LGUnknownKeyException" reason:[NSString stringWithFormat:@"****[%@ %@]: this class is not key value coding-compliant for the key name.****",self,NSStringFromSelector(_cmd)] userInfo:nil];
}

// 取值
- (nullable id)lg_valueForKey:(NSString *)key{

    // 1:刷选key 判断非空
    if (key == nil  || key.length == 0) {
        return nil;
    }

    // 2:找到相关方法 get<Key> <key> countOf<Key>  objectIn<Key>AtIndex
    // key 要大写
    NSString *Key = key.capitalizedString;

    // 拼接方法
    NSString *getKey = [NSString stringWithFormat:@"get%@",Key];
    NSString *countOfKey = [NSString stringWithFormat:@"countOf%@",Key];
    NSString *objectInKeyAtIndex = [NSString stringWithFormat:@"objectIn%@AtIndex:",Key];

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

    if ([self respondsToSelector:NSSelectorFromString(getKey)]) {
        return [self performSelector:NSSelectorFromString(getKey)];
    }else if ([self respondsToSelector:NSSelectorFromString(key)]){
        return [self performSelector:NSSelectorFromString(key)];
    }else if ([self respondsToSelector:NSSelectorFromString(countOfKey)]){
        if ([self respondsToSelector:NSSelectorFromString(objectInKeyAtIndex)]) {
            int num = (int)[self performSelector:NSSelectorFromString(countOfKey)];
            NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:1];
            for (int i = 0; i<num-1; i++) {
                num = (int)[self performSelector:NSSelectorFromString(countOfKey)];
            }

            for (int j = 0; j<num; j++) {
                id objc = [self performSelector:NSSelectorFromString(objectInKeyAtIndex) withObject:@(num)];
                [mArray addObject:objc];
            }
            return mArray;
        }
    }
#pragma clang diagnostic pop


    // 3:判断是否能够直接赋值实例变量-YES、NO
    if (![self.class accessInstanceVariablesDirectly] ) {
        @throw [NSException exceptionWithName:@"LGUnknownKeyException" reason:[NSString stringWithFormat:@"****[%@ valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.****",self] userInfo:nil];
    }

    // 4.找相关实例变量进行赋值
    // 4.1 定义一个收集实例变量的可变数组
    NSMutableArray *mArray = [self getIvarListName];

    // _<key> _is<Key> <key> is<Key>
    // _name -> _isName -> name -> isName
    NSString *_key = [NSString stringWithFormat:@"_%@",key];
    NSString *_isKey = [NSString stringWithFormat:@"_is%@",Key];
    NSString *isKey = [NSString stringWithFormat:@"is%@",Key];
    
    // 判断是否存在对应的成员变量
    if ([mArray containsObject:_key]) {
        Ivar ivar = class_getInstanceVariable([self class], _key.UTF8String);
        return object_getIvar(self, ivar);;
    }else if ([mArray containsObject:_isKey]) {
        Ivar ivar = class_getInstanceVariable([self class], _isKey.UTF8String);
        return object_getIvar(self, ivar);;
    }else if ([mArray containsObject:key]) {
        Ivar ivar = class_getInstanceVariable([self class], key.UTF8String);
        return object_getIvar(self, ivar);;
    }else if ([mArray containsObject:isKey]) {
        Ivar ivar = class_getInstanceVariable([self class], isKey.UTF8String);
        return object_getIvar(self, ivar);;
    }
    
    return @"";
}

#pragma mark **- 相关方法**

- (BOOL)lg_performSelectorWithMethodName:(NSString *)methodName value:(id)value{
    if ([self respondsToSelector:NSSelectorFromString(methodName)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self performSelector:NSSelectorFromString(methodName) withObject:value];
#pragma clang diagnostic pop
        return YES;
    }
    return NO;
}

- (id)performSelectorWithMethodName:(NSString *)methodName{

    if ([self respondsToSelector:NSSelectorFromString(methodName)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        return [self performSelector:NSSelectorFromString(methodName) ];
#pragma clang diagnostic pop
    }
    return nil;
}

- (NSMutableArray *)getIvarListName{
    NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:1];
    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([self class], &count);
    for (int i = 0; i<count; i++) {
        Ivar ivar = ivars[i];
        const char *ivarNameChar = ivar_getName(ivar);
        NSString *ivarName = [NSString stringWithUTF8String:ivarNameChar];
        NSLog(@"ivarName == %@",ivarName);
        [mArray addObject:ivarName];
    }

    free(ivars);
    return mArray;
}

@end

5. 总结

上一篇下一篇

猜你喜欢

热点阅读