Objective c 嵌套字典的访问对象

Objective c 嵌套字典的访问对象,objective-c,nsdictionary,nsmutabledictionary,Objective C,Nsdictionary,Nsmutabledictionary,在Objective-C中是否有直接访问外部词典的内部词典的方法?例如,我有一个对象的键,它是内部字典的一部分,是否有任何方法可以直接从该键访问该对象 GameDictionary { Indoor_Game = { "game1" = chess; "game2" = video_Game; "game3" = poker; }; OutDoor_Game = { "game4" = cricket; "game5" = hockey; "game6" = football; }; }

在Objective-C中是否有直接访问外部词典的内部词典的方法?例如,我有一个对象的键,它是内部字典的一部分,是否有任何方法可以直接从该键访问该对象

GameDictionary {
  Indoor_Game = { "game1" = chess; "game2" = video_Game; "game3" = poker; };
  OutDoor_Game = { "game4" = cricket; "game5" = hockey; "game6" = football; };
};
我有一个键“game4”,但我不知道该键的哪个字典对象存在,目前我必须在每个字典中搜索对象,我使用的代码是:

NSString* gameName = nil;
NSString* gameKey = @"game4";
NSArray* gameKeys = [GameDictionary allKeys];
for (int index = 0; index < [gameKeys count]; index ++) {
  NSDictionary* GameType = [GameDictionary objectForKey:[gameKeys objectAtIndex:index]];
  if ([GameType objectForKey:gameKey]) {
    gameName = [GameType objectForKey:gameKey];
    break;
  }
}
NSString*gameName=nil;
NSString*gameKey=@“game4”;
NSArray*gameKeys=[GameDictionary allKeys];
对于(int index=0;index<[gameKeys count];index++){
NSDictionary*GameType=[GameDictionary objectForKey:[gameKeys objectAtIndex:index]];
if([GameType objectForKey:gameKey]){
gameName=[GameType objectForKey:gameKey];
打破
}
}

是直接访问内部字典而不是for循环的任何简单方法。

valueForKeyPath
看起来像您想要的

[GameDictionary valueForKeyPath:@"OutDoor_Game"]
//would return a dictionary of the games - "game4" = cricket; "game5" = hockey; "game6" = football;

[GameDictionary valueForKeyPath:@"OutDoor_Game.game4"]
//would return cricket

对于收集操作,您需要完整的路径,如:-“OutDoor\u Game.game4”,但我只有一半路径是“game4”。@Mayur这是您控制的数据结构吗?您可能需要考虑重新使用它以更好地重用。比如: