ios面试题开发知识点

KVC之valueForKeyPath的总结

2018-11-20  本文已影响17人  凡几多

一、介绍

1、KVC

KVC(Key-value coding)键值编码,是可以通过对象属性名称(Key)直接给属性值(value)编码(coding),“编码”可以理解为“赋值”。这样可以免去我们调用getter和setter方法,从而简化我们的代码,也可以用来修改系统控件内部属性,KVC是KVO、Core Data、CocoaBindings的技术基础,他们都是利用了OC的动态性

  • KVC用法
    setValue:forKey:(为对象的属性赋值)
    setValue: forKeyPath:(为对象的属性赋值(包含了setValue:forKey:的功能,并且还可以对对象内的类的属性进行赋值))
    valueForKey:(根据key取值)
    valueForKeyPath:(根据keyPath取值)
    setValuesForKeysWithDictionary:(对模型进行一次性赋值)
  • 为什么可以用NSNumber来接收int、float的数据类型?
    因为:使用valueForKey:时,KVC会自动将标量值(int、float、struct等)翻入NSNumber或NSValue中包装成一个对象,然后返回。因此,KVC有自动包装功能。

2、valueForKeyPath

valueForKeyPath 方法如下:

/* Key-path-taking variants of like-named methods. The default implementation of each parses the key path enough to determine whether or not it has more than one component (key path components are separated by periods). If so, -valueForKey: is invoked with the first key path component as the argument, and the method being invoked is invoked recursively on the result, with the remainder of the key path passed as an argument. If not, the like-named non-key-path-taking method is invoked.
*/
- (nullable id)valueForKeyPath:(NSString *)keyPath;

网上对 valueForKeyPath 的讲解有很多,但是都不太全面,于是我想把这些归纳整理一个比较全的,供以后自己和大家参考学习。我一开始只是对 valueForKey 比较熟悉,对 valueForKeyPath 并没有用到过,直到有一次见到了它的用法,发现真的很强大神奇。

  • keykeyPath 的区别
    keyPath方法是集成了key的所有功能,也就是说对一个对象的一般属性进行赋值、取值,两个方法是通用的,都可以实现。但是对对象中的对象进的属性行赋值,只有keyPath能够实现。

KVC集合运算符允许在valueForKeyPath:方法中使用key path符号在一个集合中执行方法。无论什么时候你在key path中看见了@,它都代表了一个特定的集合方法,其结果可以被返回或者链接,就像其他的key path一样。

集合运算符会根据其返回值的不同分为以下三种类型:

  • 简单的集合运算符 返回的是strings,** number**, 或者 dates
  • 对象运算符 返回的是一个数组
  • 数组和集合运算符 返回的是一个数组或者集合

二、用法

1、对数组内的字典相同的key取值

对于@[@{key:value},@{key:value},@{key:value}]的数组(数组元素是字典的),通过同一个key可以取到value的集合(数组)

    NSDictionary *dic1 = @{@"city":@"北京",@"count":@"22"};
    NSDictionary *dic2 = @{@"city":@"上海",@"count":@"18"};
    NSDictionary *dic3 = @{@"city":@"深圳",@"count":@"17"};
    
    NSArray *arr = @[dic1,dic2,dic3];
    
    NSLog(@"city:%@",[arr valueForKeyPath:@"city"]);
    NSLog(@"count:%@",[arr valueForKeyPath:@"count"]);

输出结果为:

city:(
    "北京",
    "上海",
    "深圳"
)

2、数组求和sum、求平均average、求最大值max、求最小值min

对于一个由NSNumber或者数字的字符串组成的数组,可直接进行求和:

NSArray *array = @[@20, @10, @30, @4, @6, @"8"];
CGFloat sumValue = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];
NSLog(@"%f", sumValue); // 78

当然也可以从第一个例子中,获取count的值的相对应的和、平均、最大、最小值:

    NSLog(@"求和:%@",[arr valueForKeyPath:@"@sum.count"]);
    NSLog(@"平均:%@",[arr valueForKeyPath:@"@avg.count"]);
    NSLog(@"最大:%@",[arr valueForKeyPath:@"@max.count"]);
    NSLog(@"最小:%@",[arr valueForKeyPath:@"@min.count"]);

输出结果为:

count:(
    22,
    22,
    18,
    17
)

3、数组内字符串操作

如果一个数组是由字符串组成的,那么字符串有的属性,都可以直接对数组内所有的字符串统一进行操作,例如:
uppercaseString: 全部大写
同样,valueForKey 方法也可以达到此目的。

NSArray *array = @[@"aa", @"bb", @"cc"];
    
NSArray *tmp = [array valueForKeyPath:@"uppercaseString"];
NSArray *tmp2 = [array valueForKey:@"uppercaseString"];// valueForKey也可以达到相同目的

NSLog(@"%@", tmp);

输出结果为:

(
    AA,
    BB,
    CC
)

length: 获取每个字符串长度

NSArray *array = @[@"aa", @"bb", @"cc"];
NSArray *lengths = [array valueForKeyPath:@"length"];
NSLog(@"%@", lengths);

输出结果为:

(
    2,
    2,
    2
)

4、数组去重

  • 如果数组内包含重复的元素,可以直接去重:
NSArray *array2 = @[@"1", @"dd", @"ccc", @"dd", @"1", @"d", @"d"];
NSArray *rs = [array2 valueForKeyPath:@"@distinctUnionOfObjects.self"];
NSLog(@"%@", rs);

输出结果为:

(
    dd,
    d,
    1,
    ccc
)
  • 数组内包含字典,去重字典某字段的重复值

如果数组中包含字典,字典某些字段的值是重复的,那么可以直接取出去掉了重复值的内容:

NSArray *array3 = @[
                        @{@"name": @"zhangsan", @"age": @"1"},
                        @{@"name": @"zhangsan", @"age": @"1"},
                        @{@"name": @"lisi", @"age": @"2"}
                        ];
    
NSArray * arr = [array3 valueForKeyPath:@"@distinctUnionOfObjects.name"];
NSLog(@"%@", arr);

输出结果为:

(
    zhangsan,
    lisi
)

5、多级字典嵌套取值

对于 @{key1:@{key2:vale}} 的字典(字典的value是另一个字典),通过 key1.key2 的链式的方式得到最深层的字典的值。

    NSDictionary *dict4 = @{@"name":@"小明",@"age":@"22"};
    NSDictionary *dict5 = @{@"student":dict4};
    NSDictionary *dict6 = @{@"class":dict5};
    NSDictionary *dict7 = @{@"school":dict6};
    
    NSLog(@"%@",[dict7 valueForKeyPath:@"school.class.student.name"]);
    NSLog(@"%@",[dict7 valueForKeyPath:@"school.class.student.age"]);

输出结果为:

 小明
 22

6、数组内模型取值

如果数组内存放的是数据模型,同样可以取出其中某个属性的值:

    NSArray *array = @[@"aa", @"bb", @"cc"];

    NSMutableArray *peoples = [NSMutableArray arrayWithCapacity:array.count];
    for (int i = 0; i < array.count; i++) {
        People *p = [[People alloc]init];
        p.name = array[i];
        p.age = i;
        [peoples addObject:p];
    }
    NSArray *names = [peoples valueForKeyPath:@"name"];
    NSLog(@"%@", names);

输出结果为:

(
    aa,
    bb,
    cc
)

7、其他:

(1)改变 UITextfield 的 placeholder 的颜色等

[myTextField setValue:[UIColor whiteColor] forKeyPath:@”_placeholderLabel.textColor”];

比起重写 - (void)drawPlaceholderInRect:(CGRect)rect;要方便太多!

(2)在xib/Storyboard中,也可以使用KVC
下面是在xib中使用KVC把图片边框设置成圆角,但是并不会在xib里看到效果的。

valueForKey.png

目前还不能自定义集合操作符。

以上总结参考了并部分摘抄了以下文章,非常感谢以下作者的分享!:
1、作者NapoleonY 的《KVC的keyPath中的集合运算符的使用》
2、作者ImmortalSummer 的《神奇的valueForKeyPath》
3、作者流火绯瞳 的《[iOS] valueForKeyPath 使用》
4、作者HF飞哥 的《KVC 中的 valueForKeyPath 高级用法》
5、作者Mattt的 KVC Collection Operators
6、作者俊华的博客的 KVC与Runtime结合使用(案例)及其底层原理

上一篇下一篇

猜你喜欢

热点阅读