我有一个NSArray,其中包含从服务器中提取的多个NSDictionary,并且我想迭代称为NSDictionarykey's value的每个"id",然后将它们添加到NSMutableArray。我使用"for loop"来实现此功能,但是NSMutableArray登录Xcode debug area的结果非常奇怪且错误。

这是用于迭代NSDictionary中的每个NSArray的代码。

//Get the dictionary that included item ids.
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];

//Create the array add item id objects.
NSMutableArray *itemIds = [NSMutableArray array];

//Fetching item ids in dictionary array.
NSArray *itemIdsDictionary = dictionary[@"itemRefs"];

NSLog(@"%i", [itemIdsDictionary count]);//Have valid dictionary.

for(int i = 0; i < [itemIdsDictionary count]; i++)
{
   NSString *aId = (itemIdsDictionary[i])[@"id"];
   [itemIds addObject:aId];

   NSLog(@"%@", itemIds);
   NSLog(@"%@", aId);
}


NSMutableArray中的Xcode degug area登录是:

2013-07-04 22:55:26.053 Readable[3222:c07] 5
2013-07-04 22:55:26.053 Readable[3222:c07] (
    51d58c198e49d061db0011e3
)
2013-07-04 22:55:26.054 Readable[3222:c07] (
    51d58c198e49d061db0011e3,
    51d58c198e49d061db0011e4
)
2013-07-04 22:55:26.054 Readable[3222:c07] (
    51d58c198e49d061db0011e3,
    51d58c198e49d061db0011e4,
    51d5745982d493d61500e706
)
2013-07-04 22:55:26.054 Readable[3222:c07] (
    51d58c198e49d061db0011e3,
    51d58c198e49d061db0011e4,
    51d5745982d493d61500e706,
    51d5745982d493d61500e707
)
2013-07-04 22:55:26.054 Readable[3222:c07] (
    51d58c198e49d061db0011e3,
    51d58c198e49d061db0011e4,
    51d5745982d493d61500e706,
    51d5745982d493d61500e707,
    51d55fb04de3837bf3006e83
)


真的希望有人可以帮助我解决这个问题,这对我自己的应用程序来说非常重要。谢谢!!!!!!

最佳答案

尝试这个 -

    for(int i = 0; i < [itemIdsDictionary count]; i++)
    {
         NSDictionary *dictionary = [itemIdsDictionary objectAtIndex:i];
         [itemIds addObject:[dictionary objectForKey:@"id"]];
    }

    NSLog(@"itemIds : %@",itemIds);

关于ios - 如何迭代多个NSDictionary键的值之一,并将其​​添加到NSMutableArray?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17473548/

10-13 03:52