0

我有一个NSArray包含多个NSDictionary从服务器获取的内容,我想迭代每个NSDictionarykey's value调用的内容"id",然后将它们添加到NSMutableArray. 我曾经"for loop"实现过这个功能,但是NSMutableArray登录的Xcode debug area结果很奇怪,而且不正确。

NSDictionary这是在 a中迭代每个的代码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
)

真的希望有人能帮我解决这个问题,这是我自己的应用程序中非常重要的阶段。谢谢!!!!!!

4

3 回答 3

2

尝试这个 -

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

    NSLog(@"itemIds : %@",itemIds);
于 2013-07-05T10:16:23.620 回答
0

尝试

for (NSDictionary *dict in itemIdsDictionary)
    [itemIds addObject:[dict valueForKey@"id"]];
于 2013-07-04T15:11:28.933 回答
0

如果您提供字典的结构也会有所帮助。从 Web 服务返回哪些密钥?等等。

//Get the dictionary that included item ids.
NSDictionary *dictionary = [NSDictionary dictionary];

//Create the array to hold the ID's
NSMutableArray *itemIds = [NSMutableArray array];

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

[itemIdsDictionary enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
    [itemIds addObject:[dict objectForKey:@"id"]];
}];

这是我认为你要做的一个例子。

如果您想确保只有一个 ID 条目,即使您的字典中有来自 Web 服务的重复项,您也可以使用 NSMutableSet 来阻止此操作。下面是两者的例子。

//This is just an array to hold the dictionaries that you should get from the web service
NSMutableArray *arrayOfDictionariesFromWebService = [NSMutableArray array];

//Make some fake data for the example
for (int i = 0; i < 5; ++i)
{
    //Make a dictionary with a value for the key id
    NSDictionary *dictionary = @{@"id" : [NSString stringWithFormat:@"ID%d", i]};

    //Add it to the array
    [arrayOfDictionariesFromWebService addObject:dictionary];
}

//Add a duplicate item to show example of using a set
NSDictionary *duplicateObject = @{@"id" : @"ID0"};
[arrayOfDictionariesFromWebService addObject:duplicateObject];

//Create an array and a set to hold the ID's
NSMutableArray *itemIds = [NSMutableArray array];
NSMutableSet *setOfItemIds = [NSMutableSet set];

//Enumerate through the array of dictionaries to pull out the ID from the dictionary and add it to the array and set of IDs
[arrayOfDictionariesFromWebService enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
    [itemIds addObject:[dict objectForKey:@"id"]];
    [setOfItemIds addObject:[dict objectForKey:@"id"]];
}];

//If you want to sort your ID's you can sort it using sortedArrayUsingDescriptors
NSArray *sortedArrayOfItemIDs = [[setOfItemIds allObjects] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES]]];

//Log out the result
NSLog(@"Array of ID's \n%@", itemIds);
NSLog(@"Array of ID's from the set \n%@", [setOfItemIds allObjects]);
NSLog(@"Sorted array of ID's from the set \n%@", sortedArrayOfItemIDs);

控制台应如下所示。

2013-07-10 09:04:40.824 STC Companion[574:c07] Array of ID's 
(
    ID0,
    ID1,
    ID2,
    ID3,
    ID4,
    ID0
)
2013-07-10 09:04:40.825 STC Companion[574:c07] Array of ID's from the set 
(
    ID2,
    ID3,
    ID0,
    ID4,
    ID1
}
2013-07-10 09:13:08.063 STC Companion[886:c07] Sorted array of ID's from the set 
(
    ID0,
    ID1,
    ID2,
    ID3,
    ID4
)
于 2013-07-04T15:27:58.543 回答