문제

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 == %@.@max.data", array];

However this sometimes gives wrong results. Am I missing anything?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top