一、获取数组里的最大、最小、平均、求和
NSArray *array = @[@"1",@"2",@"3",@"4",@"5"];
NSNumber *sum = [array valueForKeyPath:@"@sum.floatValue"];
NSNumber *avg = [array valueForKeyPath:@"@avg.floatValue"];
NSNumber *max = [array valueForKeyPath:@"@max.floatValue"];
NSNumber *min = [array valueForKeyPath:@"@min.floatValue"];
NSLog(@"sum=%@",sum);
NSLog(@"avg=%@",avg);
NSLog(@"max=%@",max);
NSLog(@"min=%@",min);
二、过滤掉重复的数据
NSArray *array = @[@"zz",@"qwe",@"nn",@"nn",@"qwe",@"hh"];
NSArray *filterArray = [array valueForKeyPath:@"@distinctUnionOfObjects.self"];
NSLog(@"filterArray is %@",filterArray);
三、根据字典中的键的值过滤掉相同的数据
NSArray *array = @[ @{@"title":@"zxd",@"name":@"zhangxuedong1"}, @{@"title":@"zxd1",@"name":@"zhangxuedong2"}, @{@"title":@"zxd2",@"name":@"zhangxuedong3"}, @{@"title":@"zxd",@"name":@"zhangxuedong1"}];
NSArray *newArray = [array valueForKeyPath:@"@distinctUnionOfObjects.name"];
NSLog(@"%@", newArray);
四、实例化方法调用
NSArray *array = @[@"zz",@"qwe",@"nn",@"nn",@"qwe",@"hh"];
NSArray *upperArray = [array valueForKeyPath:@"uppercaseString"];
NSLog(@"upperArray%@",upperArray);
网友评论