xcode 如何迭代并获取 NSDictionary 中的所有值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8073044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How I can iterate and get all values in a NSDictionary?
提问by leoromerbric
I have a problem.
我有个问题。
I am using the XMLReader class to get a NSDictionary
and everything works fine. But i can't to get the values of the atributes of my productData element.
我正在使用 XMLReader 类来获取 aNSDictionary
并且一切正常。但是我无法获得我的 productData 元素的属性值。
Specifically, I have the following NSDictionary
:
具体来说,我有以下几点NSDictionary
:
{
response = {
products = {
productsData = (
{
alias = "Product 1";
id = 01;
price = "10";
},
{
alias = "Product 2";
id = 02;
price = "20";
},
{
alias = "Product 3";
id = 03;
price = "30";
});
};
};
}
I used this code to create de NSDictionary
:
我使用此代码创建 de NSDictionary
:
NSDictionary *dictionary = [XMLReader dictionaryForXMLData:responseData error:&parseError];
and responseData contains:
和 responseData 包含:
<application>
<products>
<productData>
<id>01</id>
<price>10</price>
<alias>Product 1</alias>
</productData>
<productData>
<id>02</id>
<price>20</price>
<alias>Product 2</alias>
</productData>
<productData>
<id>02</id>
<price>20</price>
<alias>Product 3</alias>
</productData>
</products>
</application>
Then, i don't know how get the values of each productData like id, price and alias...
然后,我不知道如何获取每个 productData 的值,例如 id、price 和 alias ...
Anybody know how to do it??
有人知道怎么做吗??
Thanks and please forgive my bad english !
谢谢,请原谅我糟糕的英语!
采纳答案by Jim
Starting with
从...开始
NSDictionary *dictionary = [XMLReader dictionaryForXMLData:responseData error:&parseError];
you can do something like this:
你可以做这样的事情:
NSDictionary *application = [dictionary objectForKey:@"application"];
if ([[application objectForKey:@"products"] isKindOfClass [NSArray class]]) {
NSArray *products = [application objectForKey:@"products"];
for (NSDictionary *aProduct in products) {
// do something with the contents of the aProduct dictionary
}
else if {[[application objectForKey:@"products"] isKindOfClass [NSDictionary class]]) {
// you will have to see what the returned results look like if there is only one product
// that is not in an array, but something along these lines may be necessary
// to get to a single product dictionary that is returned
}
I have had a case similar to this (parsing JSON) where an array was not returned for a signle value, so the result had to be checked for both an array (of product dictionaries in your case) or a single NSDictionary (the product dictionary in your case).
我有一个与此类似的情况(解析 JSON),其中没有为信号值返回数组,因此必须检查数组(在您的情况下为产品字典)或单个 NSDictionary(产品字典)的结果在你的情况下)。
回答by Oliver
NSArray* keys = [theDict allKeys];
for(NSString* key in keys) {
id obj = [theDict objectForKey:key];
// do what needed with obj
}
you could try something like this :
你可以尝试这样的事情:
NSArray* keys = [theDict allKeys];
for(NSString* key in keys) {
if ([key isEqualToString:@"product"]) {
NSArray* arr = [theDict objectForKey:key];
// do what needed with arr
}
}
回答by CherryKuczery
There is a method on NSDictionary
- -allValues
which returns a new array containing the dictionary's values. Maybe that will help.
有一个方法 on NSDictionary
--allValues
返回一个包含字典值的新数组。也许这会有所帮助。