copyItemAtPath在模拟器上运行但在设备上失败

时间:2012-05-08 08:15:34

标签: iphone objective-c ios xcode nsfilemanager

我有这个代码将.txt文件从主包复制到文档目录。这适用于模拟器,但无法在设备上工作。我通过删除文档目录中的txt文件并再次运行应用程序来验证它是否可以在Simulator上运行。当我在设备上运行应用程序时,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.)'。' * *第一次抛出调用堆栈:

1 个答案:

答案 0 :(得分:1)

Foundation/FoundationErrors.h为您提供NSFileReadNoSuchFileError = 260, // Read error (no such file)

表示在构建期间不再将文件复制到应用程序包中。

模拟器包仍然具有文件,因为在构建期间将新文件复制到那里,但是即使在执行目标Clean时也不会删除先前复制的文件。

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

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

相关问题