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

标签 iphone objective-c dictionary 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";
    }
}
}

例如,我希望能够访问 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 .

另见 this related StackOverflow question .

关于iphone - 直接访问 Objective-C 中的嵌套字典值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4317760/

相关文章:

iphone - UIBackgroundModes位置和显着位置变化随区域监控

iphone - 低优先级长时间运行任务

iphone - 如何创建可以在软件中动态着色的艺术资源?

iPhone:安全容器

iphone - 绘制 UITableviewcell 时的动画

Swift 字典默默地不存储新项目

iphone - 如何处理动态 UITableView 中对 UITextFields 的引用 (iPhone)

ios - MonoTouch <--> 静态库异常 : Could not load NIB in bundle

python - 在 Python 3.x 中将多个词典写入多个 csv 文件

python - 读取文件并与字典进行比较