![](/img/trans.png)
[英]writing an NSArray to plist works in simulator but fails in the device
[英]copyItemAtPath works on simulator but fails on device
我有這段代碼,可以將.txt文件從主捆綁包復制到文檔目錄。 這可以在模擬器上運行,但無法在設備上運行。 我通過刪除文檔目錄中的txt文件並再次運行該應用程序來驗證它在模擬器上是否有效。 當我在設備上運行應用程序時,copyItemAtPath失敗。 這是我的代碼。
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath= [documentsDirectory stringByAppendingPathComponent:@"zipFileName.txt"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
{
return;
}
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"zipFileName.txt"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
我嘗試再次清潔和建造,甚至重啟手機,但無濟於事。
錯誤 :2012-05-08 16:13:19.487 balita [162:707] *-[ViewController currentJsonFile],/ Users / diffy / Documents / balita / balita / ViewController.m中的斷言失敗 : 144 2012-05-08 16 :13:19.496 balita [162:707] 由於未捕獲的異常“ NSInternalInconsistencyException”而終止應用程序,原因:“無法創建消息為“操作無法完成”的可寫數據庫文件。 (可可錯誤260。)'。 * *第一個調用堆棧:
Foundation/FoundationErrors.h
給您NSFileReadNoSuchFileError = 260, // Read error (no such file)
這意味着在構建期間不再將文件復制到應用程序捆綁包中。
模擬器捆綁包中仍然包含該文件,因為在構建期間會將新文件復制到其中,但是即使執行目標“清理”也不會刪除以前復制的文件。
要重現模擬器問題,您可以從那里刪除應用程序(長按或通過從模擬器目錄中刪除應用程序文件夾)並重新安裝。
要解決此問題,請將文件添加到目標的“ Copy Bundle Resources
階段,並檢查文件是否已添加到項目中並存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.