Reputation: 1493
I have an NSArray
containing multiple NSDictionary
fetching from the server, and i want to iterate every NSDictionary
one of the key's value
called "id"
and then add them to a NSMutableArray
. I used "for loop"
to implement this function, but the results of the NSMutableArray
log in Xcode debug area
was very weird and incorrectly.
This is the code for iterate every NSDictionary
in 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);
}
the NSMutableArray
log in Xcode degug area
is:
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
)
Really hope someone can help me solve this problem, and this is very important phase in my own app. Thanks!!!!!!
Upvotes: 0
Views: 2236
Reputation: 1817
It would also help if you gave the structure of your dictionary. What keys are returned from the web service? ect.
//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"]];
}];
Here is an example of what I think you are tying to do.
If you want to make sure you only have one entry of an ID even if there are duplicates in your dictionary from the web service you can use a NSMutableSet to stop this. Below is the example of both.
//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);
The console should look like this.
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
)
Upvotes: 0
Reputation: 1292
Try this -
for(int i = 0; i < [itemIdsDictionary count]; i++)
{
NSDictionary *dictionary = [itemIdsDictionary objectAtIndex:i];
[itemIds addObject:[dictionary objectForKey:@"id"]];
}
NSLog(@"itemIds : %@",itemIds);
Upvotes: 2
Reputation: 500
Try
for (NSDictionary *dict in itemIdsDictionary)
[itemIds addObject:[dict valueForKey@"id"]];
Upvotes: 0