2012-08-14 42 views
0

可能重複: How to write to plist successfully?如何寫入plist文件的目標C

我嘗試寫test.plist文件,該文件被保存在我的支持文件, 這裏是我的代碼

- (IBAction)acceptAction:(id)sender { 
NSBundle *mainBundle = [NSBundle mainBundle]; 
NSString *pathToFile = [mainBundle pathForResource:@"test" ofType:@"plist"]; 
NSMutableDictionary *md = [[NSMutableDictionary alloc] initWithContentsOfFile:pathToFile]; 
[md setValue:@"yes" forKey:@"hasAgree"]; 
[md writeToFile:pathToFile atomically:YES]; 
} 


- (IBAction)declineAction:(id)sender { 
NSBundle *mainBundle = [NSBundle mainBundle]; 
NSString *pathToFile = [mainBundle pathForResource:@"test" ofType:@"plist"]; 
NSMutableDictionary *md = [[NSMutableDictionary alloc] initWithContentsOfFile:pathToFile]; 
NSString *value; 
value = [md objectForKey:@"hasAgree"]; 
NSLog(@"value is %@", value); 
} 

接受wrtting動作按鈕.. 下降對接行動對於閱讀它.. 但不工作,有什麼建議嗎?

回答

2

您不允許在您的軟件包目錄中編寫代碼。您必須改用文檔目錄。搜索一下,你會發現啓示。