Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing all data from NSMutableDictionary

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
like image 954
dispatchMain Avatar asked Dec 27 '22 06:12

dispatchMain


2 Answers

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.

like image 156
Zack Brown Avatar answered Jan 16 '23 18:01

Zack Brown


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;
like image 23
Jai Govindani Avatar answered Jan 16 '23 17:01

Jai Govindani