0

我正在尝试遍历 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;
   }
}];
4

3 回答 3

3

您的代码中有一个非常简单的错误。这一行:

NSInteger count = [key integerValue];

应该:

NSInteger count = [obj integerValue];

'key' 是颜色名称,obj是数字。正如你所拥有的,每次迭代count都设置为0,因为调用integerValue非数字字符串会给你0.

于 2013-02-07T23:18:07.977 回答
0

Simple solution using your example:

NSMutableArray *arrayNumbers = [NSMutableArray arrayWithObjects:
                                [NSNumber numberWithInt:0],
                                [NSNumber numberWithInt:1],
                                [NSNumber numberWithInt:2],
                                [NSNumber numberWithInt:6],
                                [NSNumber numberWithInt:4],
                                nil];
NSMutableArray *arrayColours =  [NSMutableArray arrayWithObjects:
                                 @"yellow",
                                 @"blue",
                                 @"green",
                                 @"purple",
                                 @"orange",
                                 nil];
NSMutableDictionary *squareColors = [NSMutableDictionary dictionaryWithObjects:arrayNumbers
                                                                       forKeys:arrayColours];
NSUInteger indexOfArray = [arrayNumbers indexOfObject:[arrayNumbers valueForKeyPath:@"@max.intValue"]];
NSLog (@"Colour with largest value is %@", [arrayColours objectAtIndex:indexOfArray]);
于 2013-02-07T23:04:11.880 回答
0

您可以将您的密钥存储到一个数组中并使用该数组进行迭代吗?这可能是最有效的解决方案,因为无论如何您都需要知道每个键。

于 2013-02-07T23:11:58.273 回答