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;
}
}];