美文网首页iOS
问题:KVC的keyPath中的集合运算符如何使用?

问题:KVC的keyPath中的集合运算符如何使用?

作者: 姜小舟 | 来源:发表于2020-05-11 15:05 被阅读0次

KVC集合运算符允许在valueForKeyPath:方法中使用集合运算符执行方法。无论什么时候你在key path中看见了@,它都代表了一个特定的集合方法。集合运算符必须用在集合对象上或普通对象的集合属性上

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

一、简单集合操作符

返回的是strings, number, 或者 dates。

  • @count: 返回一个值为集合中对象总数的NSNumber对象。
  • @sum: 首先把集合中的每个对象都转换为double类型,然后计算其- 总,最后返回一个值为这个总和的NSNumber对象。
  • @avg: 把集合中的每个对象都转换为double类型,返回一个值为平均值的NSNumber对象。
  • @max: 使用compare:方法来确定最大值。所以为了让其正常工作,集合中所有的对象都必须支持和另一个对象的比较。
  • @min: 和@max一样,但是返回的是集合中的最小值。
    实例:前言的例子中用简单集合操作符如下:
NSArray *score = @[@(89),@(80),@(20),@(56),@(89),@(100),@(89),@(89)];
NSLog(@"%@",[score valueForKeyPath:@"@count.self"]);//8
NSLog(@"%@",[score valueForKeyPath:@"@sum.self"]);//612
NSLog(@"%@",[score valueForKeyPath:@"@max.self"]);//100
NSLog(@"%@",[score valueForKeyPath:@"@min.self"]);//20
NSLog(@"%@",[score valueForKeyPath:@"@avg.self"]);//76.5

二、对象操作符

  • @unionOfObjects/ @distinctUnionOfObjects: 返回一个由操作符右边的key path所指定的对象属性组成的数组。其中@distinctUnionOfObjects会对数组去重, 而@unionOfObjects不会。
    实例:假如有一个person对象,有name,age,position等属性
Person *person1 = [[Person alloc] init];
person1.name = @"王五";
person1.age = 10;
person1.position = @"学生";

Person *person2 = [[Person alloc] init];
person2.name = @"赵六";
person2.age = 19;
person2.position = @"学生";

Person *person3 = [[Person alloc] init];
person3.name = @"张三";
person3.age = 30;
person3.position = @"公司高管";

Person *person4 = [[Person alloc] init];
person4.name = @"李四";
person4.age = 25;
person4.position = @"软件开发";

Person *person5 = [[Person alloc] init];
person5.name = @"七七";
person5.age = 19;
person5.position = @"学生";

NSArray *personArr = @[person1,person2,person3,person4,person5];
NSLog(@"%@",[personArr valueForKeyPath:@"@distinctUnionOfObjects.age"]);
#输出结果为:10, 30,  19, 25(去重)
NSLog(@"%@",[personArr valueForKeyPath:@"@unionOfObjects.age"]);
#输出结果为:10, 19, 30,25, 19(没有去重)

三、 数组和集合操作符

数组和集合操作符与对象操作符相似。只不过它是在NSArray和NSSet所组成的集合中工作的。

  • @distinctUnionOfArrays / @unionOfArrays: 返回了一个数组,其中包含这个集合中每个数组对于这个操作符右面指定的key path进行操作之后的值。distinct版本会移除重复的值。
  • @distinctUnionOfSets: 和@distinctUnionOfArrays差不多, 但是它期望的是一个包含着NSSet对象的NSSet,并且会返回一个NSSet对象。因为集合不能包含重复的值,所以它只有distinct操作。
    实例:在上述person对象的基础上,在增加一个Studentd对象,有name,age等属性
Student *student1 = [[Student alloc] init];
student1.name = @"张三";
student1.age = 25;

Student *student2 = [[Student alloc] init];
student2.name = @"呵呵";
student2.age = 30;

Student *student3 = [[Student alloc] init];
student3.name = @"李四";
student3.age = 28;
NSArray *studentArr = @[student1,student2,student3];

NSLog(@"%@",[@[studentArr,personArr] valueForKeyPath:@"@distinctUnionOfArrays.age"]);
输出结果:10, 19,28,25,30//两个数组合并去重后
NSLog(@"%@",[@[studentArr,personArr] valueForKeyPath:@"@unionOfArrays.age"]);
#输出结果:25,30, 28, 10,19, 30,25,19//两个数组合并

NSSet *studentSet = [NSSet setWithArray:studentArr];
NSSet *personSet = [NSSet setWithArray:personArr];
NSSet *allSet = [NSSet setWithObjects:studentSet,personSet, nil];
NSLog(@"%@",[allSet valueForKeyPath:@"@distinctUnionOfSets.age"]);
#输出结果:10, 19,28,25,30//和distinctUnionOfArrays效果一样,只不过是NSSet

相关文章

  • 问题:KVC的keyPath中的集合运算符如何使用?

    KVC集合运算符允许在valueForKeyPath:方法中使用集合运算符执行方法。无论什么时候你在key pat...

  • Session212-What new in Foundatio

    KeyPath 在Swift3.0中KeyPath其实都是String,且使用KeyPath使用KVC时候得到的是...

  • 了解 Key-Value Coding

    Key-Value Coding KVC的使用 对象属性的访问(基本类型、集合类型、集合操作符,(keyPath)...

  • KVC中的集合运算符

    简单实用的集合运算符 KVC中的集合运算符有以下三类: 1、简单集合运算符:@avg、@sum、@max、@min...

  • KVC集合运算符

    KVC集合运算符允许在valueForKeyPath:方法中使用key path符号在一个集合中执行方法。无论什么...

  • iOS - 关于 KVC 的一些总结

    目录1. 什么是 KVC2. 访问对象属性3. 访问集合属性4. 使用集合运算符5. 自定义集合运算符6. 非对象...

  • KVC简单使用

    1、KVC使用 2、key 和 keyPath 区别 3、获取所有同属性的值 4、利用KVC将字典数据转换为模型s...

  • KVC集合操作符

    第一次遇到是在阅读AFNetworking中NSURLSession部分代码的时候: KVC集合运算符允许以key...

  • Swift 4新知:KVC和KVO新姿势

    随着 keypath 得到增强,KVC 和 KVO 的 API 都有了一些进化。 struct 也支持 KVC 一...

  • KVC 集合运算符

    文章转自http://nshipster.cn/另外iOS 中集合遍历方法的比较和技巧 · sunnyxx的技术博...

网友评论

    本文标题:问题:KVC的keyPath中的集合运算符如何使用?

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