NSArray *allValues = @[[NSNumber numberWithInt:(int)roundf(appDelegate.itemOneValue)],
[NSNumber numberWithInt:(int)roundf(appDelegate.itemTwoValue)],
[NSNumber numberWithInt:(int)roundf(appDelegate.itemThreeValue)],
[NSNumber numberWithInt:(int)roundf(appDelegate.itemFourValue)],
[NSNumber numberWithInt:(int)roundf(appDelegate.itemFiveValue)]];
int max = [[allValues valueForKeyPath:@"@max.intValue"] intValue];
或者
NSNumber * max = [allValues valueForKeyPath:@"@max.intValue"];
一种不太灵活但可能更容易理解的替代方法:
int max = (value2 < value1) value2 : value1;
max = (value3 < max) value3 : max;
max = (value4 < max) value4 : max;
max = (value5 < max) value5 : max;
max = (value6 < max) value6 : max;
max = (value7 < max) value7 : max;
// now max contains the highest of all values.