【问题标题】:copyItemAtPath works on simulator but fails on devicecopyItemAtPath 在模拟器上工作,但在设备上失败
【发布时间】:2012-05-16 17:21:17
【问题描述】:

我有这段代码将 .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。)'。' ** 首先抛出调用栈:

【问题讨论】:

  • 您从 [错误本地化描述] 得到什么错误信息?
  • 我已经更新了我的问题,附上了错误信息。
  • 设备和模拟器之间存在区分大小写的差异。设备区分大小写。
  • user523234 是正确的。我拥有的文件名是 zipfileName.txt,我在代码中将文件称为 zipFileName.txt。现在好了。谢谢!

标签: iphone objective-c ios xcode nsfilemanager


【解决方案1】:

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

这意味着在构建过程中文件不再复制到应用程序包中。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    相关资源
    最近更新 更多