2011-08-15 81 views
1

我有以下代碼。它不會生成錯誤,但我的文件也不會更新。文件寫入問題

if([cDate compare:Today] == NSOrderedAscending){ 

    NSLog(@"%@", [NSString stringWithFormat:@"%@, 0", Today]); 
    if(![[NSString stringWithFormat:@"%@, 0", Today] writeToFile:filePath atomically:TRUE]) 
      NSLog(@"writeToFile failed"); 
} 
+0

filePath指向哪裏?它需要是一個可寫目錄,例如Documents目錄 - 而不是應用程序包。 – InsertWittyName

+0

所以我們可以假設外層if是真的?第一個NSLog被寫入? – iandotkelly

+0

文件路徑設置爲:NSString * filePath = [[NSBundle mainBundle] pathForResource:@「info」ofType:@「txt」];是的,我假設如果該方法返回一個true,該文件被寫入。這是一個錯誤的假設? – saman01

回答

1

您無法寫入捆綁包。它的代碼是已簽名和只讀的。您需要使用文檔文件夾。

使用此命令進入文檔文件夾,並在那裏寫入權限。

NSArray *basePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);