Finding maximum numeric value in NSArray

Adam S. picture Adam S. · Jun 20, 2010 · Viewed 33.4k times · Source

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.

Answer

sergiobuj picture sergiobuj · Jun 20, 2010

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