直接访问Objective-C中的嵌套字典值

use*_*889 24 iphone dictionary objective-c nsdictionary key-value-coding

有没有办法在Objective-C中直接访问外部数组的内部数组?例如,对外部数据源的调用将返回以下对象:

{
bio = "this is the profile.bio data";
"first_name" = John;
"last_name" = Doe;
location =     {
    name = "Any Town, Any State";
};
metadata =    {
    pictures =    {
        picture = "https://picture.mysite.com/picture.jpeg";
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我希望能够访问例如location.name或metadata.pictures.picture数据.但是,点符号似乎不起作用.例如:

_gfbLocation = [result objectForKey:@"location.name"];
_gfbPicture = [result objectForKey:@"metadata.pictures.picture"];
Run Code Online (Sandbox Code Playgroud)

我能够访问此数据的唯一方法是首先将内部数组的内容设置为对象.思考?

Sim*_*ker 60

对于像这样的嵌套键,您可以使用keyPath.keyPath只是一系列用点连接的键.您可以使用它们从支持键值编码的对象中检索嵌套值 - 包括像您这样的NSDictionary对象.所以在你的情况下这应该工作:

[result valueForKeyPath:@"location.name"];
Run Code Online (Sandbox Code Playgroud)

有关键值编码的更多详细信息,请参阅Apple的键值编码编程指南.

另请参阅此相关的StackOverflow问题.