有没有办法在 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";
}
}
}
例如,我希望能够访问 location.name 或 metadata.pictures.picture 数据。但是,点符号似乎不起作用。例如:
_gfbLocation = [result objectForKey:@"location.name"];
_gfbPicture = [result objectForKey:@"metadata.pictures.picture"];
我能够访问此数据的唯一方法是首先将内部数组的内容设置为对象。想法?
最佳答案
对于这样的嵌套键,您可以使用 keyPath。 keyPath 只是一系列用点连接的键。您可以使用它们从支持键值编码的对象中检索嵌套值 - 包括像您这样的 NSDictionary 对象。所以在你的情况下这应该有效:
[result valueForKeyPath:@"location.name"];
有关键值编码的更多详细信息,请参阅 Apple 的 Key-Value Coding Programming Guide .
关于iphone - 直接访问 Objective-C 中的嵌套字典值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4317760/