查找 NSArray 中的最大数值

发布于 2024-09-06 19:34:29 字数 86 浏览 2 评论 0原文

我有一个 NSNumbers 的 NSArray,想要找到数组中的最大值。是否有任何内置功能可以做到这一点?我正在使用 iOS4 GM,如果这有什么区别的话。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荭秂 2024-09-13 19:34:29

KVC 方法如下所示:

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

或者

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

将数字作为 NSArray

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

大姐,你呐 2024-09-13 19:34:29
NSArray *  test= @[@3, @67, @23, @67, @67];
int maximumValue = [[test valueForKeyPath: @"@max.self"] intValue];
 NSLog(@" MaximumValue = %d", maximumValue);

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

// Maximum = 67
十年不长 2024-09-13 19:34:29

这是swift版本

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

Here is the swift version

let maxValue =  (numbers.value(forKeyPath: "@max.self") as! Double)
香橙ぽ 2024-09-13 19:34:29

希望对您有帮助。

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文