使用 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类型 外,基本是一致的。
网友评论