更新iPhone plist中的值

时间:2011-04-14 18:30:10

标签: iphone cocoa-touch

我有一个应用程序,我希望用户能够设置一些默认值。它们存储在.plist文件中。

在应用程序上启动它会在Documents中检查文件,如果找不到,则会复制捆绑的文件。

我可以在文档中读取plist中的值,但是如果我尝试在

中更新它们
- (void)applicationDidEnterBackground:(UIApplication *)application

然后

NSMutableDictionary* updateVal=[[NSMutableDictionary alloc]initWithContentsOfFile:docPath];

使用“EXC_BAD_ACCESS”崩溃应用程序,但仅在值已更改时才会崩溃。

完整代码

- (void)applicationDidEnterBackground:(UIApplication *)application{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *docPath = [docDir stringByAppendingPathComponent:@"settings.plist"];
NNSMutableDictionary* updateVal=[[NSMutableDictionary alloc]initWithContentsOfFile:docPath];

NSNumber* defWidthSet = defaultWidthDel;
NSNumber* defAspectSet = defaultAspectDel;

[updateVal setObject:defWidthSet forKey:@"defaultWidth"];
[updateVal setObject:defAspectSet forKey:@"defaultAspect"];
[updateVal writeToFile:docPath atomically:YES];
[updateVal release];

我不知道我做错了什么。

修改

搞定了......

新代码

- (void)applicationDidEnterBackground:(UIApplication *)application{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"settings.plist"];
NSMutableDictionary* updateVal=[[NSMutableDictionary alloc]initWithContentsOfFile:docPath];

NSNumber* defWidthSet = defaultWidthDel;
NSNumber* defAspectSet = defaultAspectDel;

[updateVal setObject:defWidthSet forKey:@"defaultWidth"];
[updateVal setObject:defAspectSet forKey:@"defaultAspect"];
[updateVal writeToFile:docPath atomically:NO];
[updateVal release];

任何人都可以告诉我第一种方式出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

搞定了

- (void)applicationDidEnterBackground:(UIApplication *)application{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *docPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"settings.plist"];
NSMutableDictionary* updateVal=[[NSMutableDictionary alloc]initWithContentsOfFile:docPath];
NSNumber* defWidthSet = defaultWidthDel;
NSNumber* defAspectSet = defaultAspectDel;
[updateVal setObject:defWidthSet forKey:@"defaultWidth"];
[updateVal setObject:defAspectSet forKey:@"defaultAspect"];
[updateVal writeToFile:docPath atomically:NO];
[updateVal release];