在 Objective-C 中遍历 NSDictionary
有没有一种简单的方法可以让我从 NSDictionary 中提取特定值,而无需为我想要下降的每个级别调用 objectForKey
?
例如:
{
response = {
data = {
foo = "bar";
};
user = {
"first_name" = "Joe";
"last_name" = "Bloggs";
};
};
}
有什么简单的方法可以提取 first_name
吗?实际上,我的数据比这更加嵌套,我想提取各个不同级别的数据。
谢谢。
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?
For example:
{
response = {
data = {
foo = "bar";
};
user = {
"first_name" = "Joe";
"last_name" = "Bloggs";
};
};
}
Is there any easy way to pull out first_name
? In reality, my data is much more nested than this, and I want to pull out data at various different levels.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
键值编码是你的朋友!
使用 KVC,您应该能够执行以下操作:
这是 另一个相关问题及其答案这可能对您有帮助,这也指向另一个问题并进一步阐述。
这是
valueForKeyPath:
。Key Value Coding is your friend!
Using KVC, you should be able to do something like:
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
?它在苹果的文档中。这是针对数组的,但应该类似地工作: theocacao.com on NSArray and KVC
另外,确保您使用字符串作为密钥。你可能会遇到问题。
有关键值编码的参考: 有关键值编码的 Apple 文档
Use
valueForKeyPath
? It's in Apple's docs.This is for an array, but should work similarly: theocacao.com on NSArray and KVC
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