如何獲取數(shù)組的最大值劣摇,其實(shí)oc可以通過(guò)valueForKeyPath方法來(lái)設(shè)置你想要獲取的一些數(shù)組刷后,比如數(shù)組的最大值吃靠,最小值硫眨,平均值等。下面是實(shí)踐:
NSMutableArray* array = [NSMutableArray array];
for (int i = 0; i < 10; i++) {
CGFloat num = arc4random() % 100 + 1;
[array addObject:[NSNumber numberWithFloat:num]];
}
CGFloat maxValue = [[array valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat minValue = [[array valueForKeyPath:@"@min.floatValue"] floatValue];
重點(diǎn)在這句話上
@”@max.floatValue”(獲取最大值)巢块,
@”@min.floatValue”(獲取最小值)捺球,
@”@avg.floatValue” (獲取平均值),
@”@count.floatValue”(獲取數(shù)組大小)
等等夕冲。氮兵。。歹鱼。