Accessing a nested NSDictionary object

Is there a way to directly access the internal dictionary of an external dictionary in Objective-C? For example, I have a key to an object that is part of an internal dictionary. Is there a way to directly access an object from this key.

GameDictionary { Indoor_Game = { "game1" = chess; "game2" = video_Game; "game3" = poker; }; OutDoor_Game = { "game4" = cricket; "game5" = hockey; "game6" = football; }; }; 

I have the key "game4", but I don’t know in which dictionary this key is present, at present I have to look in each dictionary for the object, the code that I use:

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

Do they have a simple way of accessing directly to the internal dictionary, and not to loops.

+4
source share
1 answer

valueForKeyPath looks the way you want.

 [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 

https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html

+6
source

All Articles