美文网首页
KVC底层原理和实现

KVC底层原理和实现

作者: 不掉头发的程序猿 | 来源:发表于2018-07-05 17:46 被阅读0次

关于KVC

一。 valueForKey:key的调用顺序

1.先用相关方法

(1)先调用getter方法:有三种getter方法优先级是:getKey>key>isKey

(2)NSArry方法:- (NSInteger)countOfKey   - (id)objectInKeyAtIndex:(NSInteger)index

2.如果没有相关方法:看+(BOOL)accessInstanceVariablesDirectly返回值

如果返回YES,找到成员变量,先后顺序是:_key>_isKey>key>isKey

如果返回NO,抛出异常valueForUndefinedKey(此时如果重写-(id)valueForUndefinedKey:(NSString *)key就不会抛出异常)

当属性为一个对象的时候,取得属性可以通过valueForKeyPath来获取,路径通过.获取值

//    NSObject *name = [dogg valueForKey:@"person"];

//    NSString *personName = [name valueForKey:@"myName"];

    NSLog(@"%@",[dogg valueForKeyPath:@"person.myName"]);

二。setValue:forKey:的调用顺序

1.找set方法:setKey>setIsKey

2.如果没有相关方法:看+(BOOL)accessInstanceVariablesDirectly返回值

如果返回YES,找到成员变量,先后顺序是:_key>_isKey>key>isKey

如果返回NO,抛出异常valueForUndefinedKey(此时如果重写- (void)setValue:(id)value forUndefinedKey:(NSString*)key不会抛出异常),当赋值为nil的时候可以- (void)setNilValueForKey:(NSString*)key来防止抛出异常

当清楚原理,可以自己写setValue:forKey:

- (void)MT_setValue:(id)value forKey:(NSString*)key

{

    if(key ==nil|| key.length==0) {

        return;

    }

    //用setKey方法

    NSString *setKey = [NSString stringWithFormat:@"set%@:",key.capitalizedString];

    if ([self respondsToSelector:NSSelectorFromString(setKey)]) {

        [self performSelector:NSSelectorFromString(setKey) withObject:value];

        return;

    }

    //用setIsKey

    NSString *setIsKey = [NSString stringWithFormat:@"setIs%@:",key.capitalizedString];

    if ([setKey respondsToSelector:NSSelectorFromString(setIsKey)]) {

        [self performSelector:NSSelectorFromString(setIsKey) withObject:value];

        return;

    }

    //accessInstanceVariablesDirectly为NO抛出异常

    if (![self.class accessInstanceVariablesDirectly]) {

        NSException *exception = [NSException exceptionWithName:@"MTKVC exception" reason:@"accessInstanceVariablesDirectly为NO,不可以找到对于属性" userInfo:nil];

        @throwexception;

        return;

    }

    unsignedintcount =0;

    Ivar*ivars =class_copyIvarList([selfclass], &count);

    //找_key

    for(inti =0; i< count; i++) {

        Ivarivar = ivars[i];

        NSString *keyName = [NSString stringWithUTF8String:ivar_getName(ivar)];

        if([keyNameisEqualToString:[NSStringstringWithFormat:@"_%@",key]]) {

            object_setIvar(self, ivar, value);

            free(ivars);

            return;

        }

    }

    //找_isKey

    for(inti =0; i< count; i++) {

        Ivarivar = ivars[i];

        NSString *keyName = [NSString stringWithUTF8String:ivar_getName(ivar)];

        if([keyNameisEqualToString:[NSStringstringWithFormat:@"_is%@",key.capitalizedString]]) {

            object_setIvar(self, ivar, value);

            free(ivars);

            return;

        }

    }

    //找key

    for(inti =0; i< count; i++) {

        Ivarivar = ivars[i];

        NSString *keyName = [NSString stringWithUTF8String:ivar_getName(ivar)];

        if([keyNameisEqualToString:key]) {

            object_setIvar(self, ivar, value);

            free(ivars);

            return;

        }

    }

    //找isKey

    for(inti =0; i< count; i++) {

        Ivarivar = ivars[i];

        NSString *keyName = [NSString stringWithUTF8String:ivar_getName(ivar)];

        if([keyNameisEqualToString:[NSStringstringWithFormat:@"is%@",key.capitalizedString]]) {

            object_setIvar(self, ivar, value);

            free(ivars);

            return;

        }

    }

    //异常

    [self setValue:value forUndefinedKey:key];

}

valueForKey:key也可以这样写

- (id)MT_valueForKey:(NSString*)key

{

    if(key ==nil|| key.length==0) {

        returnnil;

    }

    //用getKey

    NSString *getKey = [NSString stringWithFormat:@"get%@",key.capitalizedString];

    if ([self respondsToSelector:NSSelectorFromString(getKey)]) {

      idvalue =    [self performSelector:NSSelectorFromString(getKey)];

      returnvalue;

    }

    //用key

    if ([self respondsToSelector:NSSelectorFromString(key)]) {

        idvalue =    [self performSelector:NSSelectorFromString(key)];

        returnvalue;

    }

    //用isKey

    NSString *isKey = [NSString stringWithFormat:@"is%@",key.capitalizedString];

    if ([self respondsToSelector:NSSelectorFromString(isKey)]) {

        idvalue =    [self performSelector:NSSelectorFromString(isKey)];

        returnvalue;

    }

    //accessInstanceVariablesDirectly为NO抛出异常

    if (![self.class accessInstanceVariablesDirectly]) {

        NSException *exception = [NSException exceptionWithName:@"MT_valueForKey异常" reason:@"accessInstanceVariablesDirectly为NO,不可以查找属性值" userInfo:nil];

        @throwexception;

        returnnil;

    }

    unsignedintcount =0;

    //找_key

    Ivar*ivars =class_copyIvarList([selfclass], &count);

    for(inti =0; i< count; i++) {

        Ivarivar = ivars[i];

        NSString *keyName = [NSString stringWithUTF8String:ivar_getName(ivar)];

        if([keyNameisEqualToString:[NSStringstringWithFormat:@"_%@",key]]) {

            free(ivars);

            return  object_getIvar(self, ivar);

        }

    }

    //找_isKey

    for(inti =0; i< count; i++) {

        Ivarivar = ivars[i];

        NSString *keyName = [NSString stringWithUTF8String:ivar_getName(ivar)];

        if([keyNameisEqualToString:[NSStringstringWithFormat:@"_is%@",key.capitalizedString]]) {

            free(ivars);

            return  object_getIvar(self, ivar);

        }

    }

    //找key

    for(inti =0; i< count; i++) {

        Ivarivar = ivars[i];

        NSString *keyName = [NSString stringWithUTF8String:ivar_getName(ivar)];

        if([keyNameisEqualToString:key]) {

            free(ivars);

            return  object_getIvar(self, ivar);

        }

    }

    //找iskey

    for(inti =0; i< count; i++) {

        Ivarivar = ivars[i];

        NSString *keyName = [NSString stringWithUTF8String:ivar_getName(ivar)];

        if([keyNameisEqualToString:[NSStringstringWithFormat:@"is%@",key.capitalizedString]]) {

            free(ivars);

            return  object_getIvar(self, ivar);

        }

    }

    return  [self valueForUndefinedKey:key];

}

相关文章

网友评论

      本文标题:KVC底层原理和实现

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