我有一个NSNrray的NSNumber,想要找到数组中的最大值。这样做有内置的功能吗?我使用的是iOS4 GM,如果这有任何区别的话。
答案 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)