I have an NSMutableArray, purDesc, that holds an NSMutableDictionary, purDetails. When I use NSLog(@"%@", purDesc); it returns empty spaces where there should be dictionarys as follows:
singleton purDesc Dump:(
{
},
{
},
{
},
{
},
{
}
)
Now the size/look/dump changes every time I add a dictionary to reflect the number of times a dictionary gets added/removed. Here are the code snippets taken from my XML parser:
…
else if ([elementName isEqualToString:@"purDesc"])
{
//close purDisc
// dumps dictionary into log
NSLog(@"End of purDesc");
NSLog(@"Dump:%@", [purDesc description]);
[singleton setPurDesc:purDesc];//sets this response to the response in the singleton
NSLog(@"singleton purDesc Dump:%@", [[singleton purDesc] description]);
return;//we're done here
}
else if ([elementName isEqualToString:@"PurDetails"])
{
//close purDetails
// dumps dictionary into log
NSLog(@"End of PurDetails");
[purDetails addEntriesFromDictionary:description];//sets current purDetails to current description
NSLog(@"Dump:%@", [purDetails description]);
[purDesc addObject:purDetails];//adds purDetails dictionary to purDesc
[purDetails removeAllObjects];
NSLog(@"Current purDesc:%@", [purDesc description]);
}
What am I doing wrong and why is the log coming up semi-empty?
I see that you do:
That stores a reference to purDetails in purDesc, and increments its retain count, but it does not make a copy. So if you removeAllObjects, the contents of the dictionary are lost. The fact you “stored” a reference to it in purDesc doesn’t change that. I guess what you see is the result of this or a similar action.
You can’t re-use the dictionary. You’ll have to alloc and init a new one each time. You should release the local one you have, but not clear it.
I’d need to see more code to see how you can improve your design thus that this is not a problem and memory management is taken care of properly.