OC底层原理探索之KVC原理分析

473 阅读5分钟

什么是KVC

KVC是Key-Value Coding的简称,称为键值编码。它是NSKeyValueCoding非正式协议启用的一种机制,可以使对象通过该协议来间接访问其属性,这种间接访问机制补充了实例变量及其相关的访问器方法所提供的直接访问。 ​

使用方法

  1. 一般setter 方法
    Person *person = [[Person alloc] init];
    // 一般setter 方法
    person.name      = @"张三"; // setter -- llvm
    person.age       = 18;
  1. Key-Value Coding (KVC) 非正式协议 - 间接访问
    [person setValue:@"张三" forKey:@"name"];
  1. KVC - 集合类型
    NSArray *array = [person valueForKey:@"array"];
    array = @[@"100",@"2",@"3"];
    [person setValue:array forKey:@"array"];
  1. KVC - 访问非对象属性
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"];
    NSLog(@"%@",value1); //{length = 12, bytes = 0x0000803f0000004000004040}
    
    ThreeFloats th;
    [value1 getValue:&th];
    NSLog(@"%f-%f-%f",th.x,th.y,th.z);//1.000000-2.000000-3.000000
  1. KVC - 层层访问 - keyPath
    Student *student = [Student alloc];
    student.subject    = @"subject";
    person.student     = student;
    [person setValue:@"Swift" forKeyPath:@"student.subject"];

KVC设值

KVC官方文档说明

  1. Look for the first accessor named set: or _set, in that order. If found, invoke it with the input value (or unwrapped value, as needed) and finish. 首先找这三种setter:方法,分别是:set->_set->setIs(文档没有写,但是属性有这个,所以联想到可以验证下这个方法,证实确实可以),其中是成员变量的名字, 且首字母大写,比如本文中的name,顺序就是setName->_setName->setIsName。当找到这三种setter中任意一个时,则进行赋值,如果没有找到则进入2 ​

  2. If no simple accessor is found, and if the class method accessInstanceVariablesDirectly returns YES, look for an instance variable with a name like _, _is, , or is, in that order. If found, set the variable directly with the input value (or unwrapped value) and finish. 判断+ (BOOL)accessInstanceVariablesDirectly函数是否返回YES,如果返回YES,则按照 _key->_iskey->key->iskey的顺序搜索成员,找到任意一个则进行赋值,否则进入步骤三;如果返回NO,则直接进入步骤三。 ​

  3. Upon finding no accessor or instance variable, invoke setValue:forUndefinedKey:. This raises an exception by default, but a subclass of NSObject may provide key-specific behavior. 如果setter方法 或者 实例变量都没有找到,系统会执行该对象的setValue:forUndefinedKey:函数,默认抛出异常,所以我们使用KVC进行解析模型数据时,要实现setValue:forUndefinedKey:函数,否则会崩溃。

image.png

KVC取值

  1. Search the instance for the first accessor method found with a name like get, , is, or _, in that order. If found, invoke it and proceed to step 5 with the result. Otherwise proceed to the next step. 调用getter方法,调用顺序是:get -> -> is -> _,以name为例,getName -> name -> isName -> _name,如果找到,则直接返回对应的值(可能是对象),如果找不到则进入步骤二 ​

  2. If no simple accessor method or group of collection access methods is found, and if the receiver's class method accessInstanceVariablesDirectly returns YES, search for an instance variable named _, is, , or is, in that order. If found, directly obtain the value of the instance variable and proceed to step 5. Otherwise, proceed to step 6. 判断+ (BOOL)accessInstanceVariablesDirectly函数是否返回YES,如果为YES,则访问成员变量,顺序为:->_is->-> is,以name为例则是_name ->_isName->name-> isName,找到则返回对应的值(可能是对象),找不到则进入步骤三;如果返回的是NO,则截止进入步骤三 ​

  3. If all else fails, invoke valueForUndefinedKey:. This raises an exception by default, but a subclass of NSObject may provide key-specific behavior. 均找不到,则调用valueForUndefinedKey:抛出异常。

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

image.png

根据协议规则自定义KVC--设值

@interface NSObject (LGKVC)
// LG KVC 自定义入口
- (void)lg_setValue:(nullable id)value forKey:(NSString *)key;
@end
- (void)lg_setValue:(nullable id)value forKey:(NSString *)key{
   
    // KVC 自定义
    // 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:判断是否能够直接赋值实例变量
    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];
    
}

根据协议规则自定义KVC--取值

- (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 @"";
}

补充:performSelector

是否能响应该方法

- (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;
}

补充:getIvarListName

获取所有实例变量的名字:

- (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;
}

补充:Method Swizzling

交换之后: image.png

    class func fc_swizzleInstance(oriSel: Selector, swizzleSel: Selector) {
        
        guard let oriMethod = class_getInstanceMethod(self, oriSel) else {
            return
        }
        
        guard let swizzleMethod = class_getInstanceMethod(self, swizzleSel) else {
            return
        }
        fc_swizzleMethod(oriSel: oriSel, oriMethod: oriMethod, swizzleSel: swizzleSel, swizzleMethod: swizzleMethod, cls: self)
    }
    
    class func fc_swizzleClass(oriSel: Selector, swizzleSel: Selector) {
        guard let oriMethod = class_getClassMethod(self, oriSel) else {
            return
        }
        
        guard let swizzleMethod = class_getClassMethod(self, swizzleSel) else {
            return
        }
        fc_swizzleMethod(oriSel: oriSel, oriMethod: oriMethod, swizzleSel: swizzleSel, swizzleMethod: swizzleMethod, cls: self)
    }
    
    private class func fc_swizzleMethod(oriSel: Selector, oriMethod: Method, swizzleSel: Selector, swizzleMethod: Method, cls: AnyClass) {
        // 方法-->实现代码
        // 添加方法
        let didAddMethod = class_addMethod(cls, oriSel, method_getImplementation(swizzleMethod), method_getTypeEncoding(swizzleMethod))
        if didAddMethod {
            // 添加成功,交换实现
            class_replaceMethod(cls, swizzleSel, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod))
        } else {
            // 交换实现
            method_exchangeImplementations(oriMethod, swizzleMethod)
        }
    }