Reputation: 1617
Maybe its a very common and nerd kind of question. I am having an NSMutableDictionary to which I am adding some objects and then adding the dictionary to NSMutableArray. After adding the dictionary to array, I am removing all objects of dictionary and setting it to nil which is removing the data from array to which I added the dictionary. Is there something wrong that I am doing which is causing the array to loose the data??
NSMutableDictionary *lDict = [[NSMutableDictionary alloc]init];
[lDict setObject:lDate forKey:@"date"];
[lDict setObject:presentationId forKey:@"pptId"];
[lDict setObject:presentationName forKey:@"PresentationName"];
[lDict setObject:presentationDesc forKey:@"PresentationDesc"];
[lDict setObject:presentationThumb forKey:@"presentationThumb"];
[lDict setObject:postCallRowId forKey:@"postCallId"];
[lDict setObject:userReaction forKey:@"userReaction"];
[lDict setObject:appDel.gRegion forKey:@"Region"];
[lDict setObject:@"Presentation" forKey:@"ContentType"];
[durationArray addObject:lDict];
//here I am having data in array
[lDict removeAllObjects];
lDict=nil;
//here I lost data in array also
Upvotes: 4
Views: 11426
Reputation: 6028
How about something like this:
NSMutableDictionary *lDict = [[NSMutableDictionary alloc]init];
[lDict setObject:lDate forKey:@"date"];
[lDict setObject:presentationId forKey:@"pptId"];
[lDict setObject:presentationName forKey:@"PresentationName"];
[lDict setObject:presentationDesc forKey:@"PresentationDesc"];
[lDict setObject:presentationThumb forKey:@"presentationThumb"];
[lDict setObject:postCallRowId forKey:@"postCallId"];
[lDict setObject:userReaction forKey:@"userReaction"];
[lDict setObject:appDel.gRegion forKey:@"Region"];
[lDict setObject:@"Presentation" forKey:@"ContentType"];
//make a new dictionary and save it
[durationArray addObject:[[NSDictionary alloc] initWithDictionary:lDict]];
[lDict removeAllObjects];
lDict=nil;
Before you remove all the objects from your mutable dictionary, make a new dictionary with the same contents and save this to your array.
By creating a new dictionary you can still maintain the key/value pairs setup in your original dictionary rather than just saving the values.
Upvotes: 8
Reputation: 3211
Going top-down, the array holds a pointer to the dictionary, which then holds pointers to all the objects that you added. Adding the dictionary to the array doesn't mean that they array has pointers to the individual objects - the array only knows about the dictionary, and only the dictionary knows the objects. When you remove all the objects from the dictionary, the array doesn't see that anything has changed. As far as it's concerned, it still has a pointer to a dictionary.
If what you're trying to do is to add objects from the dictionary into the array, then you should iterate over the dictionary and add the individual objects to the array one-by-one.
for (NSString *key in lDict)
{
[durationArray addObject:[lDict objectForKey:key]];
}
[lDict removeAllObjects];
lDict=nil;
You should still have the objects in your array at this point.
If you want to add all at once you can try:
[durationArray addObjectsFromArray:[lDict allValues]];
[lDict removeAllObjects];
lDict = nil;
Upvotes: 2
Reputation: 14169
Is there a specific reason for cleaning the NSMutableDictionary
? As mentioned by others, the NSArray
holds pointers to the objects, so these are "cleaned" as well.
One way to prevent this is to add a copy of your NSDictionary
to the NSArray
using [durationArray addObject:[lDict copy]];
(or call [lDict mutableCopy]
if it should be a mutable one).
If you are adding dictionaries in a loop, just create the NSDictionary
inside this loop and not outside.
Upvotes: 0