我有一个应用程序,可以通过网络API从mysql服务器获取一些json数据,如下所示:

[[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"streamGames", @"command", nil] onCompletion:^(NSDictionary *json) {

    //got stream

     [[API sharedInstance] setGames:[json objectForKey:@"result"]];


这给了我:

{
    awayScor = 3;
    data = "2014-04-11";
    gameType = 1;
    homeScor = 2;
    homeTeam = "Herning Blue Fox";
    time = "21:00";
},
    {
    awayScor = 1;
    data = "2014-04-08";
    gameType = 2;
    homeScor = 2;
    homeTeam = "SønderjyskE";
    time = "19:00";
},


现在,我要执行的操作是创建一个新的NSDictionary,其中仅添加数据,例如gameType = 1,因此我有一个用于蚀刻gameType的新NSDictionary。

最佳答案

如果我正确理解了这个问题!

NSArray *games; // Get this from somewhere
NSMutableDictionary *gameTypes = [NSMutableDictionary dictionary];

for (NSDictionary *game in games) {
    NSNumber *gameType = game[@"gameType"];
    NSMutableArray *gamesForType = gameTypes[gameType];
    if (!gamesForType) {
        gamesForType = [NSMutableArray array];
        gameTypes[gameType] = gamesForType;
    }
    [gamesForType addObject:game];
}


现在gameTypes将是游戏类型到该类型游戏的字典。

10-04 17:13