我是否有一种简单的方法可以从NSDictionary中提取特定值,而不会为我想要关闭的每个级别调用objectForKey
?
例如:
{
response = {
data = {
foo = "bar";
};
user = {
"first_name" = "Joe";
"last_name" = "Bloggs";
};
};
}
有没有简单的方法来提取first_name
?实际上,我的数据比这更嵌套,我想在不同的层次上提取数据。
感谢。
答案 0 :(得分:5)
Key Value Coding是你的朋友!
使用KVC,您应该可以执行以下操作:
NSString * name = [response valueForKeyPath:@"user.first_name"];
此处还another related question with answers that may help you points to another question with further elaboration。
以下是valueForKeyPath:
的文档。
答案 1 :(得分:2)
使用valueForKeyPath
?这是Apple的文档。
这适用于数组,但应该类似:theocacao.com on NSArray and KVC
另外,请确保使用字符串作为键。你可能会遇到问题。
有关键值编码的参考:Apple Docs on Key-Value Coding