我正在尝试遍历 NSMutableDictionary,但我似乎无法得到我想要的。我有一个字典将字符串映射到这样的颜色......
squareColors = [NSMutableDictionary dictionaryWithObjects: [NSMutableArray arrayWithObjects:
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:0],
nil]
forKeys: [NSMutableArray arrayWithObjects:
@"yellow",
@"blue",
@"green",
@"purple",
@"orange",
nil]];
随着时间的推移,每个条目的价值都会增加。每隔一段时间,我就想查查字典并选择计数最高的颜色。我该怎么做?这是我正在尝试的,但我不熟悉块。
__block int mostSquares = 0;
__block NSString* color = @"";
/* Look through the dictionary to find the color with the most number of squares */
[squareColors enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@ => %@", key, obj);
NSInteger count = [key integerValue];
if (count > mostSquares)
{
color = key;
mostSquares = count;
}
}];