2011-06-16 72 views
1

我必須在我的plist文件中修改存儲在bundle中的BOOL值。我能夠訪問我必須修改的字典。從nslogging中我可以看到該字典用新的值,但問題是,當我檢查的plist捆綁是不是被modified.any線索,它爲什麼不更新的plist修改一個plist不起作用

NSString* plistPath = nil; 
     NSFileManager* manager = [NSFileManager defaultManager]; 
     if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"TopicsList.plist"]) 
     { 
      if ([manager isWritableFileAtPath:plistPath]) 
      { 
       NSMutableArray* dictarrays = [NSMutableArray arrayWithContentsOfFile:plistPath]; 
       NSMutableDictionary *dict=[dictarrays objectAtIndex:indexPath.row]; 
       NSLog(@"%@ ",dict); 

       [dict setObject:[NSNumber numberWithBool:YES] forKey:@"isPaid"]; 

       NSLog(@"%@ ",dict); 
       [dict writeToFile:plistPath atomically:NO]; 
        NSLog(@"%@ ",dict); 
       [manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:&error]; 
      } 
     } 

回答

5

是plist中的資源的一部分嗎?不知道我們是否可以在那裏編輯plist。將plist複製到應用程序的Documents文件夾並在那裏更新。

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *plistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"TopicsList.plist"]; 

success = [fileManager fileExistsAtPath:plistPath]; 
if(!success){ 
    //file does not exist. So look into mainBundle 
    NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TopicsList.plist"]; 
    success = [fileManager copyItemAtPath:defaultPath toPath:plistPath error:&error]; 
} 

現在無論您需要對plist進行哪些更改或從plist讀取數據,請從Documents文件夾中的副本中讀取它。

+0

和@Nayefc這樣就意味着我應該在Xcode中刪除我的plist的參考和保存的plist在我的項目文件夾? – sujith1406 2011-06-16 08:32:24

+1

保留在那裏。在應用程序啓動時,將plist從您的包中複製到應用程序的Document文件夾(如果它尚不存在)。用相關的代碼更新我的答案(應該在應用程序中去:didFinishLaunchingWithOptions: – lostInTransit 2011-06-16 10:09:56

0

您必須將plist存儲在文檔目錄中。之後,您還必須在離開應用程序後保存plist。否則,plist在主包中,不能修改。

0

是否有任何錯誤

請與

NSError *error = nil 
[dict writeToFile:plistPath atomically:YES encoding:NSASCIIStringEncoding error:&error]; 

if (error) { 
    NSLog(@"Error: %@", [error description]); 
} else { 
NSLog(@"Success"); 
}