2011-04-14 72 views
0

我有一個應用程序,我希望用戶能夠設置一些默認值。它們存儲在.plist文件中。在iPhone中更新值plist

在應用程序啓動時,它檢查文件中的文件,如果找不到它,它將複製捆綁的文件。

我可以閱讀下列文件中的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]; 

誰能告訴我什麼是錯的我在做這第一種方式?

+0

只是一個猜測,但原子寫入可能失敗,因爲該應用程序是進入後臺進程完成之前。 – NWCoder 2011-04-14 20:42:25

回答

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];