Ios 如何从密钥为x的NSDictionary创建新的NSDictionary
我有一个应用程序,通过web api从mysql服务器获取一些json数据,如下所示:Ios 如何从密钥为x的NSDictionary创建新的NSDictionary,ios,json,Ios,Json,我有一个应用程序,通过web api从mysql服务器获取一些json数据,如下所示: [[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"streamGames", @"command", nil] onCompletion:^(NSDictionary *json) { //got stream [[API sharedInstance]
[[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,这样我就有了一个新的NSDictionary,用于蚀刻gameType。如果我正确理解了这个问题
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
将是一个游戏类型字典,用于该类型的数组游戏