OGeek|极客世界-中国程序员成长平台

标题: iphone - 将文件从 bundle 复制到文档不起作用(Cocoa 错误 512) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:25
标题: iphone - 将文件从 bundle 复制到文档不起作用(Cocoa 错误 512)

我试图简单地将我的 sqlite3 数据库复制到文档目录。我得到一个 Cocoa Error 512 并且我认为它不是一个有效的目录(或类似的东西。

数据库文件位于 XCode 中的 Resources 文件夹中。 (文件名正确)

这是我尝试使用的代码:

-(void) checkAndCreateDatabase
{
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];

    //databasePath + databaseName is declared in the header
    databaseName = @"WaypointDatabase.sql";
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    success = [fileManager fileExistsAtPath:databasePath];
if(success)
    {
        NSLog(@"Database exists");
            return;
    }
    else
        NSLog(@"Database does not exists");

    NSString *databasePathFromApp = [[NSBundle mainBundle] pathForResource"WaypointDatabase" ofType"sql"];
    if(databasePathFromApp == nil)
    {
        NSLog(@"ERROR: IT IS NIL");
    }
    NSError* error = nil;

    NSLog(@"ath in bundle:\n%@\n\n", databasePathFromApp);
    NSLog(@"ath to copy to:\n%@\n\n", databasePath);

    [fileManager copyItemAtPath:databasePathFromApp
                     toPath:databasePath error:&error];
    [fileManager release];
    if (error)
    {
        NSLog(@"%@\n\n", error);
        NSLog(@"%@", [error userInfo]);
    }
}

我得到的控制台输出是:

2011-10-30 10:36:13.242 xxxx[6726:707] Database does not exists
2011-10-30 10:36:13.249 xxxx[6726:707] Path in bundle:
/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/xxxx.app/WaypointDatabase.sql

2011-10-30 10:36:13.252 xxxx[6726:707] Path to copy to:
/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents/WaypointDatabase.sql

2011-10-30 10:36:13.268 xxxx[6726:707] Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xe8b7cd0 {NSUserStringVariant=( Copy ), NSFilePath=/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/xxxx.app/WaypointDatabase.sql, NSDestinationFilePath=/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents/WaypointDatabase.sql, NSUnderlyingError=0xe8b7ee0 "The operation couldn’t be completed. Not a directory"}

2011-10-30 10:36:13.272 xxxx[6726:707] { NSDestinationFilePath = "/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents/WaypointDatabase.sql"; NSFilePath = "/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/xxxx.app/WaypointDatabase.sql"; NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=20 \"The operation couldn\U2019t be completed. Not a directory\""; NSUserStringVariant = ( Copy ); }

我认为这一定与获取目录路径或其他东西有关,但现在已经卡了几天了。

请注意,这可以完美运行,并且在模拟器上没有任何错误。

我哪里出错了?

[更新]

这可能是因为 Documents 文件夹目录不存在吗?我将如何创建/检查它?

[更新 2]

我已经使用了一些其他测试

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *err = nil;
NSArray *dirArr = [fileManager contentsOfDirectoryAtPath:documentsDir error:&err];
NSLog(@"~~Contents:\n%@",dirArr);
NSLog(@"~~Error: \n%@",err);

令人惊讶的是,它给出了 Cocoa Error 256 并且还说“不是目录”。几乎就像 Documents 目录不存在一样。但确实如此,根据NSSearchPathForDirectoriesInDomains`

这是我得到的输出

2011-10-30 10:59:16.934 xxxx[6774:707] ~~Contents:
(null)
2011-10-30 10:59:16.936 xxxx[6774:707] ~~Error:
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x10052580 {NSUserStringVariant=( Folder ), NSFilePath=/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents, NSUnderlyingError=0x10054a50 "The operation couldn’t be completed. Not a directory"}



Best Answer-推荐答案


通过在 die info.plist 中设置非标准的 Bundle ID 解决了问题

我为此特定应用使用了 iTunes Connect 中的 Bundle ID。现在一切正常。

关于iphone - 将文件从 bundle 复制到文档不起作用(Cocoa 错误 512),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7941821/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4