I'm loading a plist file using a slightly tweaked version of the example in the developer docs
- (void) loadPlistIntoMemory
{
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:@"bullionrun.plist"];
// check to see if the plist exists in the documents directory, if not, get it from the bundle
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
NSLog(@"Property List not found. Looking for property list in Bundle.");
NSString * plistBundlePath = [[NSBundle mainBundle] pathForResource:@"bullionrun" ofType:@"plist"];
// Copy it to the document folder and read it from there
[[NSFileManager defaultManager] copyItemAtPath:plistBundlePath toPath:plistPath error:nil];
}else {
NSLog(@"Property List found in Documents Folder");
}
// Read the data in the plist
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
// If Error
if (!temp) {
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}else {
NSLog(@"Property List OK");
// store the plist data
[self setPlistData:temp];
}
}
Now, that works fine, but when I need to access PlistData later on, it's been deallocated. The header file looks like this
@interface Registry : NSObject {
NSDictionary * _plistData;
}
@property (nonatomic, retain) NSDictionary * plistData;
and the @synthesize bit looks like this @synthesize plistData = _plistData;
.
I struggle with memory management all the time. I just can't get my head around it (I'm not a natural programmer at all, I just like it), But I thought that by using retain
in the header file when I declare the plistData
property, that it would be retained.
Clearly not though, since any attempt to access it later (eg NSLog(@"%@", self.plistData)
) results in an error...
-[CFString hash]: message sent to deallocated instance 0x6436560
I've also tried creating a manual setPlistData
method that chucks a retain in there, but that crashes the app without an error.
Can anyone give me a nudge in the right direction?