iOS NSArray 快速求总和、最大值、最小值、平均值的方法

时间:2022-07-27 15:13:18

    NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil];

    CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];

    CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];

    CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];

    CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];

    NSLog(@"%f\n%f\n%f\n%f",sum,avg,max,min);