[英]Traversing NSDictionary in Objective-C
Is there an easy way for me to pull out a particular value from an NSDictionary, without calling objectForKey
for each level I want to go down? 有没有一种简单的方法可以从NSDictionary中提取特定值,而无需为每个我想要关闭的级别调用objectForKey
?
For example: 例如:
{
response = {
data = {
foo = "bar";
};
user = {
"first_name" = "Joe";
"last_name" = "Bloggs";
};
};
}
Is there any easy way to pull out first_name
? 是否有任何简单的方法来拔出first_name
? In reality, my data is much more nested than this, and I want to pull out data at various different levels. 实际上,我的数据比这更嵌套,我希望在各种不同的级别提取数据。
Thanks. 谢谢。
Key Value Coding is your friend! 关键价值编码是你的朋友!
Using KVC, you should be able to do something like: 使用KVC,您应该可以执行以下操作:
NSString * name = [response valueForKeyPath:@"user.first_name"];
Here's another related question with answers that may help you which also points to another question with further elaboration . 这是另一个相关的问题,答案可以帮助你 ,这也指出了另一个问题,进一步阐述 。
Here is the documentation for valueForKeyPath:
. 这是valueForKeyPath:
的文档valueForKeyPath:
。
Use valueForKeyPath
? 使用valueForKeyPath
? It's in Apple's docs. 这是Apple的文档。
This is for an array, but should work similarly: theocacao.com on NSArray and KVC 这是一个数组,但应该类似: NSArray和KVC上的theocacao.com
Also, make sure you're using strings as the key. 另外,请确保使用字符串作为键。 You can run into problems. 你可能会遇到问题。
For reference on Key-Value Coding: Apple Docs on Key-Value Coding 有关键值编码的参考: Apple Docs on Key-Value Coding
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.