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