gpt4 book ai didi

objective-c - Nested NSDictionary 的访问对象

转载 作者:太空狗 更新时间:2023-10-30 03:50:30 24 4
gpt4 key购买 nike

有没有办法在 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;
}
}

他们是否有直接访问内部字典而不是 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

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

关于objective-c - Nested NSDictionary 的访问对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18974631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com