NSPredicateとCocoa Predicatesを使って、配列内の最大値・最小値を取ってみました。
@max, @minはKey-Value Coding時に使用できる関数で、NSDictionaryのようなKey-Value Objectが格納されている配列でしか使えないよう。
サンプル
NSArray* dics = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:50] forKey:@"score"], [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:50] forKey:@"score"], [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:75] forKey:@"score"], [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:45] forKey:@"score"], [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:60] forKey:@"score"], [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:40] forKey:@"score"], [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:80] forKey:@"score"],nil]; NSLog(@"score = %@", [dics filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.score == %@.@max.score", dics]]); /* score = ( { score = 80; } ) */
配列をfor文で回さなくていいのは便利ですが、このくらいのものなら一長一短な感じがします…