I am stuck debugging a NSInvalidArgumentException. My latest suspicion is that I didn’t retain the data read from plist properly so that it’s occupied by some other object while I access it.
My plist structure is very complicated, it has 8 levels of arrays/dictionaries. I think I lost the memory when I try to access the lowest object.
I wonder if I have to retain every data element when I read the plist file or is it sufficient to just retain the top level object?
This is how I read:
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSError *error = [[[NSError alloc] init] autorelease];
NSArray *temp = (NSArray *)[[NSPropertyListSerialization
propertyListWithData:plistXML
options:NSPropertyListMutableContainersAndLeaves
format:nil
error:&error] retain];
self.dataPackage = [temp objectAtIndex:0];
dataPackage is declared as:
@interface rootViewController:UIViewController{
NSDictionary *dataPackage;
}
@property (retain) NSDictionary *dataPackage;
and synthesized:
@synthesize dataPackage;
Am I doing it right?
Thanks
Leo
I noticed 3 things:
You don’t need to create an NSError object! NSPropertyListSerialization will return an error object if something fails. Just init with:
NSError *error = nil;You don’t have to retain the (autoreleasing) temp-array, you obviously don’t need the whole array after fetching the object at index 0.
[temp objectAtIndex:0]will crash when the array is empty!Be sure to release the property var in dealloc with
self.dataPackage = nil. Then everything is safe from memory management perspective.