开发者

How do I read and write to plist files in objective-c? (iPhone SDK)

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:@"FSS开发者_开发百科ettings.plist"];
 NSMutableDictionary* plistDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
 [plistDictionary setObject:value forKey:key];
 [plistDictionary writeToFile:finalPath atomically:YES];
 [path release];
}


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜