美文网首页
iOS 底层原理 - KVC

iOS 底层原理 - KVC

作者: yan0_0 | 来源:发表于2020-06-21 21:19 被阅读0次

    概述

    KVC (Key-Value Coding), 也就是键值编码,是利用 NSKeyValueCoding 非正式协议实现的一种机制, 对象采用这种机制来提供对其属性的间接访问。如果想要看详细的解释,可以看下官方文档。KVC官方文档

    使用方式

    1.基本类型

    LGPerson *person = [[LGPerson alloc] init];
    [person setValue:@"KC" forKey:@"name"];
    [person setValue:@19 forKey:@"age"];
    [person setValue:@"酷C" forKey:@"myName"];
    NSLog(@"%@ - %@ - %@",[person valueForKey:@"name"],[person valueForKey:@"age"],[person valueForKey:@"myName"]);
    

    2.集合类型

    LGPerson *person = [[LGPerson alloc] init];
    person.array = @[@"1",@"2",@"3"];
    // 由于不是可变数组 - 无法做到
    // person.array[0] = @"100";
    NSArray *array = [person valueForKey:@"array"];
    // 方法一 :用 array 的值创建一个新的数组
    array = @[@"100",@"2",@"3"];
    [person setValue:array forKey:@"array"];
    NSLog(@"%@",[person valueForKey:@"array"]);
    
    // 方法二 :
    NSMutableArray *ma = [person mutableArrayValueForKey:@"array"];
    ma[0] = @"10";
    NSLog(@"%@",[person valueForKey:@"array"]);
    

    3.访问非对象属性

    ThreeFloats floats = {1., 2., 3.};
    NSValue *value  = [NSValue valueWithBytes:&floats objCType:@encode(ThreeFloats)];
    [person setValue:value forKey:@"threeFloats"];
    NSValue *reslut = [person valueForKey:@"threeFloats"];
    NSLog(@"%@",reslut);
        
    ThreeFloats th;
    [reslut getValue:&th] ;
    NSLog(@"%f - %f - %f",th.x,th.y,th.z);
    

    4.嵌套属性访问

        LGStudent *student = [[LGStudent alloc] init];
        student.subject    = @"iOS";
        person.student     = student;
        [person setValue:@"大师班" forKeyPath:@"student.subject"];
        NSLog(@"%@",[person valueForKeyPath:@"student.subject"]);
    

    KVC原理分析

    KVC 设值过程

    KVC设值会调用setValue:forKey 方法:
    1.先判断是否存在 setKey,或者_setKey,setIsKey这三种方法,如果找到立即调用相应方法并传递name参数设值,结束
    2.如果没有找到这三种方法,判断accessInstanceVariablesDirectly方法返回值,如果返回YES,依次判断_key,_is<Key>,key,isKey等成员变量是否存在,根据顺序存在即赋值并结束。
    3.如果accessInstanceVariablesDirectly方法,返回NO,或者上述所有成员变量均不存在,则调用setValue:forUndefinedKey:方法抛出异常并崩溃。

    KVC取值过程

    KVC取值会调用ValueForKey方法
    1.按照顺序查找实例方法: get<Key>, <key>, is<Key>, 或者 <key>,如果找到就跳到第五步
    2.判断是否是数组,如果是数组则查找countOf<Key>,objectIn<Key>AtIndex:或<key>AtIndexes,并返回一个新的数组。否则就执行步骤3
    3.判断是否属于NSSet,基于是否有NSSet相关的方法:countOf<Key>, enumeratorOf<Key>或 and memberOf<Key>:
    4.如果上述方法都不存在,判断对象的类方法accessInstanceVariablesDirectly 返回值,如果返回YES,按顺序查找成员变量
    <key>, _is<Key>, <key>, 或 is<Key>,如果找到直接获取实例变量的值并跳到5继续执行,否则执行6
    5.检索属性值,如果是指针对象,直接返回结果;如果该值是可转化为NSNumber类型的值,那么将该值转化为NSNumber并返回;除此以外将该值转化为NSValue类型的值作为结果返回。
    6.如果所有的方法均失败,则调用valueForUndefinedKey: 并抛出异常。

    KVC异常处理

    当我们在使用 setValue:forKey: 或者 valueForKey: 的时候,由于 key 需要自己手写且没有提示,所以很可能会不小心写错,然后就会报 setValue:forUndefinedKey: 或者 valueForUndefinedKey: 的崩溃。但是NSObject的子类可以通过重写这个方法来提供特定的操作。比如:

    - (void)setValue:(id)value forUndefinedKey:(NSString *)key{
        NSLog(@" %@ 没有这个key",key);
    }
    
    - (id)valueForUndefinedKey:(NSString *)key{
        NSLog(@" %@ 没有这个key - 给你一个其他的吧!",key);
        return @"Master";
    }
    

    使用细节

    1.针对Int类型的key的写法
    [object setValue:@123 forKey:@"count"];
    2.如果对Int类型的传一个NSString的值如:[object setValue:@"123" forKey:@"count"],系统会自动帮我们转换成__NSCFNumber的类型,说明KVC具有自动转型的功能。

    [person setValue:@"20" forKey:@"age"]; // int - string
    NSLog(@"%@-%@",[person valueForKey:@"age"],[[person valueForKey:@"age"] class]);//__NSCFNumber
    //打印结果
    2020-06-21 20:31:03.379902+0800 004-KVC异常小技巧[2098:140868] 20-__NSCFNumber
    

    3.结构体的取值以及设值要转换为NSValue作为中间媒介。

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

    4.setNilValueForKey:的方法只针对NSNumber(int,bool, etc..)以及NSValue(结构体)相关的数据生效,针对指针对象赋值nil并不会走到这个方法。

    [person setValue:nil forKey:@"age"]; // subject不会走 - 官方注释里面说只对 NSNumber - NSValue
    [person setValue:nil forKey:@"subject"];
    

    调用这个方法会走下面这个方法,发现subject不会走,subject是NSString类型,age是int类型

    - (void)setNilValueForKey:(NSString *)key{
        NSLog(@" %@ 是空值",key);
    }
    

    自定义KVC

    思路:
    首先创建一个NSObject的扩展,根据上述分析的过程,自定义setValue:forKey:以及ValueForKey:的方法。

    - (void)lg_setValue:(nullable id)value forKey:(NSString *)key{
        
        // 1:非空判断一下
        if (key == nil  || key.length == 0) return;
        
        // 2:找到相关方法 set<Key> _set<Key> setIs<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:判断是否能够直接赋值实例变量
        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>
        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:判断是否能够直接赋值实例变量
        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;
    }
    

    相关文章

      网友评论

          本文标题:iOS 底层原理 - KVC

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