iOS NSArray求数组的平均数
// 初始化数组
NSArray *numArray = [NSArray arrayWithObjects:@(20.0), @(-2.3), @(13.0),@(0.0), nil];
// 求和
NSNumber *sum = [numArray valueForKeyPath:@"@sum.floatValue"];
// 平均值
NSNumber *avg= [numArray valueForKeyPath:@"@avg.floatValue"];
// 最大值
NSNumber *max =[numArray valueForKeyPath:@"@max.floatValue"];
// 最小值
NSNumber *min =[numArray valueForKeyPath:@"@min.floatValue"];
// 创建空数组
NSArray *emptyArray = [NSArray array];
// 验证空数组使用
NSNumber *emptySum = [emptyArray valueForKeyPath:@"@sum.floatValue"];
NSLog(@"%@,%@,%@,%@,%@",sum,avg,max,min,emptySum);
打印结果
30.7, 7.675, 20, -2.3, 0