在NSArray中查找最大数值

Ada*_* S. 53 iphone objective-c nsarray

我有NSNr的NSNrray,并希望在数组中找到最大值.这样做有内置的功能吗?我使用iOS4 GM,如果这有任何区别.

ser*_*buj 176

KVC方法如下所示:

int max = [[numbers valueForKeyPath:@"@max.intValue"] intValue];
Run Code Online (Sandbox Code Playgroud)

要么

NSNumber * max = [numbers valueForKeyPath:@"@max.intValue"];
Run Code Online (Sandbox Code Playgroud)

数字作为NSArray

  • 事实上,最好使用`@"@ max.self"`.由于`@max`使用`compare:`,它需要实际的对象:使用`intValue`,不仅会丢失精度,而且`valueForKeyPath:`必须重新创建NSNumbers才能使用.作为奖励,它也适用于`NSString`s或任何实现`compare:`的东西. (14认同)