2012-05-08 58 views
2

我有这样的代码,从主包复制.txt文件到文件目录。这在模拟器上工作,但无法在设备上工作。我通过删除文档目录中的txt文件并再次运行应用程序来验证它在Simulator上的工作。当我在设备上运行应用程序时,copyItemAtPath失败。这是我的代码。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]); 
    } 

我试过清洗和再次建设,我甚至重新启动手机,但没有任何工作。

ERROR:2012-05-08 16:13:19.487 balita [162:707] *断言故障 - [视图控制器currentJsonFile],/Users/diffy/Documents/balita/balita/ViewController.m: 144 2012-05-08 16:13:19.496 balita [162:707] 由于未捕获的异常'NSInternalInconsistencyException'导致终止应用程序,原因:'无法用消息创建可写的数据库文件'操作无法完成。 (可可错误260.)'。' * *第一掷调用堆栈:

+0

你从[错误localizedDescription]得到什么错误信息? – Vin

+0

我已更新我的问题,附上错误消息。 – Diffy

+3

设备和模拟器之间存在区分大小写区别。设备区分大小写。 – user523234

回答

1

Foundation/FoundationErrors.h给你NSFileReadNoSuchFileError = 260, // Read error (no such file)

这意味着该文件的兴建新的过程中不会被复制到应用程序包。

仿真程序包仍然有文件,因为在构建过程中将新文件复制到此处,但即使在执行目标“清理”时也不会删除以前复制过的文件。

要重现模拟器的问题,您可以从那里删除一个应用程序(或长按或从模拟器目录中删除应用程序文件夹)并重新安装。

要解决该问题,请将文件添加到目标的Copy Bundle Resources阶段,并检查该文件是否已添加到项目并存在。

相关问题