Finding maximum numeric value in NSArray

微笑、不失礼 提交于 2019-11-30 06:12:09

问题


I have an NSArray of NSNumbers and want to find the maximum value in the array. Is there any built in functionality for doing so? I am using iOS4 GM if that makes any difference.


回答1:


The KVC approach looks like this:

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

or

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

with numbers as an NSArray




回答2:


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

// Maximum = 67



回答3:


Here is the swift version

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



回答4:


Hope will helpful to you.

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);


来源:https://stackoverflow.com/questions/3080540/finding-maximum-numeric-value-in-nsarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!