Reputation: 920
How to find largest value from NSArray
of NSDictionary
?
Here is my code,
NSDictionary *d1 = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:35] forKey:@"data"];
NSDictionary *d2 = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:52] forKey:@"data"];
NSArray *array = [NSArray arrayWithObjects:d1, d2, nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.data == %@[email protected]", array];
However this sometimes gives wrong results. Am I missing anything?
Upvotes: 2
Views: 2414
Reputation: 10096
Try this way for NSNumber values
NSNumber *max = [array valueForKeyPath:@"@max.data"];
and
NSNumber *max = [array valueForKeyPath:@"@max.data.floatValue"];
for NSString/NSNumbers mixed values.
Upvotes: 9