views:

73

answers:

3

I'm having problems reading and writing to a plist file. I can read the data OK, but I cant write it.. also, I think I have a memory management issues relating to this. Here's my code:

+(NSString *) getSettingString: (NSString *)key
{
 NSString *path = [[NSBundle mainBundle] bundlePath];
 NSString *finalPath = [path stringByAppendingPathComponent:@"FSSettings.plist"];
 NSMutableDictionary* plistDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
 NSString *value =  [plistDictionary objectForKey:key];
 [path release];
 return value;
}

+(void) setSettingString: (NSString *)key value:(NSString *)value
{
 NSString *path = [[NSBundle mainBundle] bundlePath];
 NSString *finalPath = [path stringByAppendingPathComponent:@"FSSettings.plist"];
 NSMutableDictionary* plistDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
 [plistDictionary setObject:value forKey:key];
 [plistDictionary writeToFile:finalPath atomically:YES];
 [path release];
}
+1  A: 

You only have to release resources if you explicitly take ownership of it, or call a copy, init, retain method. Build & Analyze in Xcode can help spot these potential memory issues, but suffice to say, loose the call to [path release] you don't own it, therefore you shouldn't release it.

jer
+2  A: 

You're trying to write the plist back into your application bundle, which is not allowed. You need to write the file into the documents directory. Details on how to locate this directory can be found here:

http://developer.apple.com/iphone/library/documentation/iphone/conceptual/iphoneosprogrammingguide/StandardBehaviors/StandardBehaviors.html#//apple_ref/doc/uid/TP40007072-CH4-SW11

Jason Foreman
A: 

bundle is read only, you should write the file in the documents directory.

Tony