美文网首页
记录KVC 对集合类型的应用

记录KVC 对集合类型的应用

作者: chaeyeonyanse | 来源:发表于2018-01-17 15:23 被阅读0次

使用 KVC 可以对特定数组内容进行求和、求最大值等操作。
Apple 官方地址:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/CollectionOperators.html#//apple_ref/doc/uid/20002176-BAJEAIEE

使用

  • -(nullable id)valueForKeyPath:(NSString *)keyPath;
    keyPath需要遵循特定的格式为:


    image.png

    当应用于集合类型如 NSArray时,left key path 可以忽略不写。
    right key path 是指集合中的数据的某一种属性,比如 floatValue,除了对数组计算count不需要指定 right key path,其他的集合操作符均需要指定。

@interface Model: NSObject

@property(nonatomic, strong) NSString *name;
@property(nonatomic, assign) CGFloat   price;
@property(nonatomic, assign) NSInteger amount;
@property(nonatomic, strong) NSArray  *hobbies;

@end
Model *m1 = [[Model alloc] initWithName:@"aaa" price:12.5 amount:5 hobbies:@[@"骑马",@"摔跤",@"滑雪"]];
Model *m2 = [[Model alloc] initWithName:@"bbb" price:1.5 amount:1 hobbies:@[@"琴",@"棋",@"书",@"画"]];
Model *m3 = [[Model alloc] initWithName:@"aaa" price:10 amount:6 hobbies:@[@"偷东西"]];
Model *m4 = [[Model alloc] initWithName:@"ddd" price:6.66 amount:7 hobbies:@[@"iOS 编程",@"画"]];
    
Model *m5 = [[Model alloc] initWithName:@"ccc" price:1999 amount:9 hobbies:@[@"排球",@"羽毛球",@"书"]];
Model *m6 = [[Model alloc] initWithName:@"fff" price:0 amount:1 hobbies:@[@"游泳"]];

NSArray *array1 = [[NSArray alloc] initWithObjects:m1,m2,m3,m4, nil];
NSArray *array2 = [[NSArray alloc] initWithObjects:m5,m6, nil];
NSArray *bigArray = [[NSArray alloc] initWithObjects:array1,array2, nil];

1、Aggregation Operators 聚合运算符,对数组做某种运算,返回一个特定的值,共五个。
@sum,@avg,@max,@min,@count
针对对数组计算 count 的情况,会返回一个 NSNumber 实例,包含数组的个数

//对price求平均值
NSNumber *avg = [array1 valueForKeyPath:@"@avg.price"];
NSLog(@"avg = %@",avg);
//avg = 7.665000000000001

@avg.price 中,price 为集合元素的某种属性,这里指的是 Model 类型中的 price 属性
@avg 即是集合操作符,注意前面的@符号不要掉了,如果记不住这些固定字符串,可以使用下面的方式来写:

//对amount求和
NSNumber *sum = [array1 valueForKeyPath:[NSString stringWithFormat:@"@%@.amount",NSSumKeyValueOperator]];
NSLog(@"sum = %@",sum);
//sum = 19
// 对price求最大值
NSNumber *max = [array1 valueForKeyPath:@"@max.price"];
NSLog(@"max = %@",max);
//max = 12.5
    
// 对amount求最小值
NSNumber *min = [array1 valueForKeyPath:@"@min.amount"];
NSLog(@"min = %@",min);
//min = 1
// 求数组数量,这是唯一不需要 right key path 的 operator
NSNumber *count = [array1 valueForKeyPath:@"@count"];
NSLog(@"count = %@",count);
//count = 4

2、Array Operators 返回数组的某个属性的所有值的数组,共两个。
@distinctUnionOfObjects
@unionOfObjects

// name 去重后的数组 @[NSString]
NSArray *distinctNameObjects = [array1 valueForKeyPath:@"@distinctUnionOfObjects.name"];
NSLog(@"distinctNameObjects = %@",distinctNameObjects);
/*
distinctNameObjects = (
    bbb,
    aaa,
    ddd
)
*/
    
//price 不去重的数组,即所有的 price 值的数组 @[NSNumber]
NSArray *priceObjects = [array1 valueForKeyPath:@"@unionOfObjects.price"];
NSLog(@"priceObjects = %@",priceObjects);
/*
priceObjects = (
    "12.5",
    "1.5",
    10,
    "6.66"
)
*/
    
//hobbies 不去重的数组,即所有的 hobbies 值的数组 @[NSArray]
NSArray *hobbiesObjects = [array1 valueForKeyPath:@"@unionOfObjects.hobbies"];
NSLog(@"hobbiesObjects = %@",hobbiesObjects);
/*
hobbiesObjects = (
        (
        "骑马",
        "摔跤",
        "滑雪"
    ),
        (
        "琴",
        "棋",
        "书",
        "画"
    ),
        (
        "偷东西"
    ),
        (
        "iOS 编程",
        "画"
    )
)
*/

3、Nesting Operators 对数组中的数组进行运算的操作符,共四个。
@distinctUnionOfArrays
@unionOfArrays

//获取所有 name 属性的所有值,去重,其数据类型为 @[NSString];
NSArray *distinctNameArray = [bigArray valueForKeyPath:@"@distinctUnionOfArrays.name"];
NSLog(@"distinctNameArray = %@",distinctNameArray);
/*
distinctNameArray = (
    ccc,
    bbb,
    aaa,
    fff,
    ddd
)
*/
    
//获取所有 hobbies 属性的所有值,其数据类型为 @[NSArray];
NSArray *hobbiesArray = [bigArray valueForKeyPath:@"@unionOfArrays.hobbies"];
NSLog(@"hobbiesArray = %@",hobbiesArray);
/*
hobbiesArray = (
        (
        "骑马",
        "摔跤",
        "滑雪"
    ),
        (
        "琴",
        "棋",
        "书",
        "画"
    ),
        (
        "偷东西"
    ),
        (
        "iOS 编程",
        "画"
    ),
        (
        "排球",
        "羽毛球",
        "书"
    ),
        (
        "游泳"
    )
)
*/
    
 //考虑下面的用法, 取到的是 array1 所有 hobbies 所包含的全部元素
NSArray *hobbiesArray2 = [array1 valueForKeyPath:@"@unionOfArrays.hobbies"];
NSLog(@"hobbiesArray2 = %@",hobbiesArray2);
// 对比一下上面hobbiesArray获得的内容,同样的操作符,操作于不同的集合类型,返回的数据的区别
/*
hobbiesArray2 = (
    "骑马",
    "摔跤",
    "滑雪",
    "琴",
    "棋",
    "书",
    "画",
    "偷东西",
    "iOS 编程",
    "画"
)
*/

@distinctUnionOfSets
@unionOfSets
这两种,与应用于 NSArray 的那两种,除了需要应用于 NSSet及返回 NSSet类型 外,基本是一致的。

相关文章

  • 记录KVC 对集合类型的应用

    使用 KVC 可以对特定数组内容进行求和、求最大值等操作。Apple 官方地址:https://developer...

  • 了解 Key-Value Coding

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

  • iOS底层原理之kvc原理分析

    1.kvc:Key-Value Coding 基本类型使用 集合类型使用 非对象类型,转换成相应的NSValue,...

  • 集合应用类型

    Object 目前为止,大多数的引用对象都是Object。有两种方式创建Object对象,第一种就是使用new对象...

  • KVC KVO高阶应用

    KVC KVO高阶应用 KVC KVO高阶应用

  • 我的Swift的学习总结 -->第二周

    集合 集合:Set,定义一个集合可以写成:var 集合名 : Set<集合类型> = [集合元素],具体的集合应用...

  • iOS KVC Collection Operators(集合操

    KeyWords: KVC valueForKeyPath: KVC集合操作符的概念 引自官方文档When you...

  • KVC,KVO

    KVC , KVO KVC和KVO的区别及应用 KVC/KVO原理 1. KVC键值编码 KVC,即是指NSKey...

  • 03--KVC/KVO本质04--KVC 注意事项

    总结 KVC 使用的一些注意事项 KVC 注意事项 KVC 自动转换类型使用string存基本类型,系统会存为 _...

  • iOS KVC运用

    主要应用场景 KVC 属性赋值 添加和访问私有成员变量(ivar) 字典转模型 取值 模型转字典 集合操作符 自定...

网友评论

      本文标题:记录KVC 对集合类型的应用

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