Reputation: 8957
I had an IPhone application in which i had an array of elements,But i need each element inside a dictionary,i.e. (nsmuatblearray of dictionaries),I did like this But some times its giving some unwanted results `
id objectInstance;
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
for (objectInstance in self.SelectedContacts)
[mutableDictionary setObject:objectInstance forKey:@"tag"];
[finalarray addObject:mutableDictionary];
` I am doing like this Can anybody points me where i am going wrong?
Upvotes: 0
Views: 75
Reputation: 2636
Not 100% sure from your question if you want an array of dictionaries each with the same key, or an array with 1 dictionary in it.
Many dictionaries version
for (long i = 0; i < [self.SelectedContacts count];i++)
{
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
[mutableDictionary setObject:[self.SelectedContacts objectAtIndex:i] forKey:@"tag"];
[finalarray addObject:mutableDictionary];
}
1 dictionary version
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
for (long i = 0; i < [self.SelectedContacts count];i++)
{
NSString *keyString = [NSString stringWithFormat:@"tag_%li",i];
[mutableDictionary setObject:[self.SelectedContacts objectAtIndex:i] forKey:keyString];
}
[finalarray addObject:mutableDictionary];
in this 1 dictionary version the tags will be tag_ followed by the old index value in the array.
tag_0 tag_1 etc.
Upvotes: 0
Reputation: 23053
Here no need to create NSMutableDictionary
because you just use it to save it in array no further modification is there.
Also it will consume more memory than NSDictionary
. In future if you want to update object in dictionary that you can convert NSDictionary
to NSMutableDictionary
using
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:object];
and vice versa to NSDictionary
.
Right now a simple way to add dictionary in array like this:
NSMutableArray *finalArray = [NSMutableArray new];
for (NSDictionary *object in self.SelectedContacts) {
[finalArray addObject:@{ @"tag" : object }];
}
Upvotes: 0
Reputation: 7343
The way you are doing it is overwriting the element for the key "tag" every time. I am not entirely sure what are you trying to accomplish, but I suspect you should write something like:
for (id objectInstance in self.SelectedContacts) {
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
[mutableDictionary setObject:objectInstance forKey:@"tag"];
[finalarray addObject:mutableDictionary];
}
Upvotes: 2
Reputation: 46563
In the loop you are putting different object with same tag name "tag". So here only the last objectInstance is getting saved, rest all are replaced by next one.
If you want each one of them to be saved, then update your "tag" with new tag-names.
Another thing, either you missed to show {
in code above or if you really don't have those braces {
and }
for for
then only next one line is taken up with the loop and [finalarray addObject:mutableDictionary];
is executed once!!!
Upvotes: 1