美文网首页
[OC基础]KVC

[OC基础]KVC

作者: zhulang424 | 来源:发表于2017-08-02 03:10 被阅读19次

    key-value coding,使用字符串更改对象状态

    介绍
    • 用途:
      1.访问私有属性
      2.整体操作(计算)
      3.批处理对象属性
    • 原理:根据传入的字符串去对象内寻找相应属性的 getter/setter,如果没有,寻找对象内以“_字符串”命名的实例变量
    • 特点:自动装箱(valueForKey:),自动开箱(setValue:forKey:
    • 缺点:
      1.需要解析字符串,速度较慢
      2.编译器无法进行错误检查
    用法
    • valueForKey:setValue:forKey:
    ZWQPerson *personA = [[ZWQPerson alloc] initWithName:@"David"
                                             phoneNumber:@12345678911
                                                 friends:nil];
    personA.item = item1;
    
    ZWQPerson *personB = [[ZWQPerson alloc] initWithName:@"Lee"
                                             phoneNumber:@12345678912
                                                 friends:nil];
    personB.item = item2;
    
    ZWQPerson *personC = [[ZWQPerson alloc] initWithName:@"Levis"
                                             phoneNumber:@12345678913
                                                 friends:@[personA,personB]];
    personC.item = item3;
    
    // KVC
    NSString *personName = [personC valueForKey:@"name"];
    NSLog(@"%@",personName); // Levis
    
    [personC setValue:@"Wade"
               forKey:@"name"];
    NSLog(@"%@",personC.name); // Wade
    
    • 带键路径(路径深度任意) valueForKeyPath:setValue:forKeyPath:
    // 带键路径的 KVC
    NSString *itemName = [personC valueForKeyPath:@"item.itemName"];
    NSLog(@"%@",itemName); // c
    
    [personC setValue:@"abc"
           forKeyPath:@"item.itemName"];
    NSLog(@"%@",personC.item.itemName); // abc
    
    • 整体操作
    // 对数组进行整体操作
    NSArray *persons = @[personA,personB,personC];
    NSArray *nameOfPersons = [persons valueForKey:@"name"];
    NSLog(@"%@",nameOfPersons); // (David,Lee,Wade)
    
    
    NSArray *nameOfFriends = [personC valueForKeyPath:@"friends.name"];
    NSLog(@"%@",nameOfFriends); // (David,Lee)
    
    • 整体操作时运算
    // 整体操作时进行运算(@sum,@avg,@min,@max,@distinckUnionOfObjects)
    NSNumber *sumOfPhoneNumber = [persons valueForKeyPath:@"@sum.phoneNumber"];
    NSLog(@"%@",sumOfPhoneNumber); // 37037036736
    
    NSNumber *sumOfFriendsPhoneNumber = [personC valueForKeyPath:@"friends.@sum.phoneNumber"];
    NSLog(@"%@",sumOfFriendsPhoneNumber); // 24691357823
    
    • 批处理
    // 批处理
    NSArray *keys = @[@"name",@"phoneNumber"];
    NSDictionary *personMessages = [personC dictionaryWithValuesForKeys:keys];
    NSLog(@"%@",personMessages); // {name = Wade;phoneNumber = 12345678913;}
    
    NSString *newPersonName = @"Adam";
    NSNumber *newPhoneNumber = @12345678900;
    NSDictionary *newPersonMessages = @{@"name" : newPersonName , @"phoneNumber" : newPhoneNumber};
    [personC setValuesForKeysWithDictionary:newPersonMessages];
    NSLog(@"%@  %@",personC.name,personC.phoneNumber); // Adam  12345678900
    
    • 防御式编程 setNilValueForKeysetValue:forUndefinedKey:valueForUndefinedKey:
    // 用来接收未定义键值的字典
    @property (nonatomic,strong) NSMutableDictionary *valuesForUndefinedKeys;
    
    ...
    
    // 接受 nil 值的特殊处理
    - (void)setNilValueForKey:(NSString *)key{
        
        if ([key isEqualToString:@"itemNumber"]) {
            self.itemNumber = 0;
        } else {
            [super setNilValueForKey:key];
        }
    }
    
    // 接收未定义的键的处理
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key{
        
        [self.valuesForUndefinedKeys setObject:value forKey:key];
    }
    
    // 取未定义键值的处理
    - (id)valueForUndefinedKey:(NSString *)key{
        
        return [self.valuesForUndefinedKeys objectForKey:key];
    }
    

    相关文章

      网友评论

          本文标题:[OC基础]KVC

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