集合操作符
NSArray *array = @[@10, @20, @30, @40];
//求和
NSNumber *sum = [array valueForKeyPath:@"@sum.self"];
NSLog(@"sum = %ld",sum.integerValue);
//平均值
NSNumber *avg = [array valueForKeyPath:@"@avg.self"];
NSLog(@"avg = %ld",avg.integerValue);
//元素个数
NSNumber *count = [array valueForKeyPath:@"@count"];
NSLog(@"count = %ld",count.integerValue);
//最大值
NSNumber *max = [array valueForKeyPath:@"@max.self"];
NSLog(@"max = %ld",max.integerValue);
//最小值
NSNumber *min = [array valueForKeyPath:@"@min.self"];
NSLog(@"min = %ld",min.integerValue);
对象操作符
NSArray *array = @[@"123", @"123", @"456", @"789", @"012"];
//返回一个数组
NSArray *arr1 = [array valueForKeyPath:@"@unionOfObjects.self"];
NSLog(@"arr1 = %@",arr1);
//返回一个去重的数组
NSArray *arr2 = [array valueForKeyPath:@"@distinctUnionOfObjects.self"];
NSLog(@"arr2 = %@",arr2);
网友评论