了解 Key-Value Coding

KVC官方文档

KVC的使用

对象属性的访问w

基本类型集合类型集合操作符,(keyPath))

对象属性的访问

消息传递

  NSArray *array = @[@"A",@"Bb",@"Ccc",@"Ddd"];
  NSArray *lenStr= [array valueForKeyPath:@"length"];
  NSLog(@"%@",lenStr);// 消息从array传递给了string  (1,2,3,4)
  NSArray *lowStr= [array valueForKeyPath:@"lowercaseString"];
  NSLog(@"%@",lowStr); //a bb ccc ddd

字典与模型转换

 NSDictionary* dict = @{
                           @"name":@"C",
                           @"age":@18,
                           @"length":@180
                           };
Person *p = [[Person alloc] init];
  // 字典转模型
[p setValuesForKeysWithDictionary:dict];
 NSLog(@"%@",p);
 // 键数组转模型到字典
 NSArray *array = @[@"name",@"age"];
 NSDictionary *dic = [p dictionaryWithValuesForKeys:array];
 NSLog(@"%@",dic);//{@"age":@18,@"length":@"180"}

聚合操作符

    NSMutableArray *personArray = [NSMutableArray array];
    for (int i = 0; i < 6; i++) {
        Person *p = [Person new];
        NSDictionary* dict = @{
                               @"name":@"C",
                               @"age":@(18+i),
                               @"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);

数组操作符(@unionOfObjects,@distinctUnionOfObjects)

    NSMutableArray *personArray = [NSMutableArray array];
    for (int i = 0; i < 6; i++) {
        Person *p = [Person new];
        NSDictionary* dict = @{
                               @"name":@"C",
                               @"age":@(18+i),
                               @"length":@(175 + 2*arc4random_uniform(6)),
                               };
        [p setValuesForKeysWithDictionary:dict];
        [personArray addObject:p];
    }
    NSLog(@"%@", [personArray valueForKey:@"length"]);
    // 返回操作对象指定属性的集合
    NSArray* arr1 = [personArray valueForKeyPath:@"@unionOfObjects.length"];
    NSLog(@"arr1 = %@", arr1);
    // 返回操作对象指定属性的集合 -- 去重
    NSArray* arr2 = [personArray valueForKeyPath:@"@distinctUnionOfObjects.length"];
    NSLog(@"arr2 = %@", arr2);

嵌套集合操作符(@distinctUnionOfArrays, @unionOfArrays)


    NSMutableArray *personArray = [NSMutableArray array];
    for (int i = 0; i < 6; i++) {
        Person *p = [Person new];
        NSDictionary* dict = @{
                               @"name":@"C",
                               @"age":@(18+i),
                               @"length":@(175 + 2*arc4random_uniform(6)),
                               };
        [p setValuesForKeysWithDictionary:dict];

    }
    NSMutableArray *personArray2 = [NSMutableArray array];
    for (int i = 0; i < 6; i++) {
        Person *p = [Person new];
        NSDictionary* dict = @{
                               @"name":@"C",
                               @"age":@(18+i),
                               @"length":@(175 + 2*arc4random_uniform(6)),
                               };
        [p setValuesForKeysWithDictionary:dict];
        [personArray2 addObject:p];
    }

    // 嵌套数组
    NSArray* nestArr = @[personArray1, personArray2];

    NSArray* arr = [nestArr valueForKeyPath:@"@distinctUnionOfArrays.length"];
    NSLog(@"arr = %@", arr);

    NSArray* arr1 = [nestArr valueForKeyPath:@"@unionOfArrays.length"];
    NSLog(@"arr1 = %@", arr1);

setValue:forKey:设值流程

  1. 当调用setValue:forKey:设置属性value时,首先查找是否有这三种setter方法,按照查找顺序为set:-> _set -> setIs,如果有其中任意一个setter方法,则直接设置属性的value
  2. 如果都没有,则查找accessInstanceVariablesDirectly是否返回YES,如果返回YES,则查找间接访问的实例变量进行赋值,查找顺序为:_, _is, , 和 is
  3. 如果都没有,会执行该对象的setValue:forUndefinedKey:方法,默认抛出NSUndefinedKeyException类型的异常

valueForKey:取值流程

  1. 当调用valueForKey:获取value时,首先查找是否有这三种setter方法,按照查找顺序为get, , is, 和 _
  2. 如果没有,在实例中搜索countOf objectIn AtIndex :(对应于NSArray类定义的原始方法)和 AtIndexes :(对应于NSArray方法objectsAtIndexes :)的模式。
  3. 如果未找到,则查找名为countOf enumeratorOf memberOf 的三种方法:(对应于NSSet类定义的原始方法)。
    如果找到所有三个方法,则创建一个响应所有NSSet方法的集合代理对象,并返回该对象。
  4. 如果都没有,则查找accessInstanceVariablesDirectly是否返回YES,如果返回YES,则查找间接访问的实例变量进行取值,查找顺序为:_ -> _is -> -> is
  5. 如果检索到的属性值是对象指针,则只需返回结果。
    如果该值是NSNumber支持的标量类型,则将其存储在NSNumber实例中并返回它。
    如果结果是NSNumber不支持的标量类型,则转换为NSValue对象并返回该对象。
  6. 如果都没有,会执行该对象的valueForUndefinedKey:方法,默认抛出NSUndefinedKeyException类型的异常

自定义KVC

按照官方文档提供的流程,可以仿照思路实现setValue:forKey:valueForKey:

- (void)cc_setValue:(nullable id)value forKey:(NSString *)key{

    // KVC 自定义
    // 1: 判断什么 key
    if (key == nil || key.length == 0) {
        return;
    }

    // 2: setter set: or _set,
    // 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 cc_performSelectorWithMethodName:setKey value:value]) {
        NSLog(@"*********%@**********",setKey);
        return;
    }else if ([self cc_performSelectorWithMethodName:_setKey value:value]) {
        NSLog(@"*********%@**********",_setKey);
        return;
    }else if ([self cc_performSelectorWithMethodName:setIsKey value:value]) {
        NSLog(@"*********%@**********",setIsKey);
        return;
    }

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

}


- (nullable id)cc_valueForKey:(NSString *)key{

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

    // 2:找到相关方法 get  countOf  objectInAtIndex
    // 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 _is  is
    // _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)cc_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

你可能感兴趣的:(了解 Key-Value Coding)