gpt4 book ai didi

iphone - copyItemAtPath 在模拟器上工作但在设备上失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:51:35 27 4
gpt4 key购买 nike

我有这段代码可以将 .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:1442012-05-08 16:13:19.496 balita[162:707] 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法创建可写数据库文件消息 '操作无法完成。 ( cocoa 错误 260.)'.'** 首先抛出调用堆栈:

最佳答案

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

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

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

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

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

关于iphone - copyItemAtPath 在模拟器上工作但在设备上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10494994/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com