在NSArray中查找最大数值

时间:2010-06-20 18:58:25

标签: iphone objective-c nsarray

我有一个NSNrray的NSNumber,想要找到数组中的最大值。这样做有内置的功能吗?我使用的是iOS4 GM,如果这有任何区别的话。

4 个答案:

答案 0 :(得分:176)

KVC方法如下:

int max = [[numbers valueForKeyPath:@"@max.intValue"] intValue];

NSNumber * max = [numbers valueForKeyPath:@"@max.intValue"];

将数字作为NSArray

答案 1 :(得分:1)

NSArray *  test= @[@3, @67, @23, @67, @67];
int maximumValue = [[test valueForKeyPath: @"@max.self"] intValue];
 NSLog(@" MaximumValue = %d", maximumValue);

// Maximum = 67

答案 2 :(得分:0)

希望对你有所帮助。

NSArray *  arrayOfBarGraphValues = @[@65, @45, @47 ,@87 , @46, @66  ,@77  ,@47  ,@79  ,@78  ,@87  ,@78  ,@87 ];
int maxOfBarGraphValues = [[arrayOfBarGraphValues valueForKeyPath: @"@max.self"] intValue];
NSLog(@" MaximumValue Of BarGraph  = %d", maxOfBarGraphValues);

答案 3 :(得分:0)

这是 swift 版本

let maxValue =  (numbers.value(forKeyPath: "@max.self") as! Double)