我有一个NSArray
包含从服务器获取的多个NSDictionary
,我希望迭代NSDictionary
中key'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
)
是:
{{1}}
真的希望有人可以帮我解决这个问题,这是我自己的应用中非常重要的阶段。感谢!!!!!!
答案 0 :(得分:2)
试试这个 -
for(int i = 0; i < [itemIdsDictionary count]; i++)
{
NSDictionary *dictionary = [itemIdsDictionary objectAtIndex:i];
[itemIds addObject:[dictionary objectForKey:@"id"]];
}
NSLog(@"itemIds : %@",itemIds);
答案 1 :(得分:0)
尝试
for (NSDictionary *dict in itemIdsDictionary)
[itemIds addObject:[dict valueForKey@"id"]];
答案 2 :(得分: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
)