Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将文件从捆绑包复制到无法工作的文档(Cocoa错误512)_Iphone_Ios_Cocoa Touch - Fatal编程技术网

Iphone 将文件从捆绑包复制到无法工作的文档(Cocoa错误512)

Iphone 将文件从捆绑包复制到无法工作的文档(Cocoa错误512),iphone,ios,cocoa-touch,Iphone,Ios,Cocoa Touch,我试图简单地将sqlite3数据库复制到documents目录。我得到了一个Cocoa错误512,我认为它不是一个有效的目录(或者类似的东西) 数据库文件位于XCode中的“我的资源”文件夹中。(文件名正确) 以下是我尝试使用的代码: -(void) checkAndCreateDatabase { BOOL success; NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *doc

我试图简单地将sqlite3数据库复制到documents目录。我得到了一个
Cocoa错误512
,我认为它不是一个有效的目录(或者类似的东西)

数据库文件位于XCode中的“我的资源”文件夹中。(文件名正确)

以下是我尝试使用的代码:

-(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(@"Path in bundle:\n%@\n\n", databasePathFromApp);
    NSLog(@"Path 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]数据库不存在
2011-10-30 10:36:13.249 xxxx[6726:707]捆绑中的路径:
/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/xxxx.app/WaypointDatabase.sql

2011-10-30 10:36:13.252 xxxx[6726:707]要复制到的路径:
/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents/WaypointDatabase.sql

2011-10-30 10:36:13.268 xxxx[6726:707]错误域=nscocaerorrordomain代码=512“操作无法完成。(Cocoa错误512。)”用户信息=0xe8b7cd0{NSUserStringVariant=( 复制 ),NSFilePath=/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/xxxx.app/WaypointDatabase.sql,NSDestinationFilePath=/var/mobile/Applications/FF654016-4257-47BB-99FE-Documents/WaypointDatabase.sql,NSUnderlyingError=0xe8b7ee0“操作无法完成。没有目录”}

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\”操作无法完成。不是目录\”; NSUserStringVariant=( 复制 ); }

我想这一定与获取目录路径或其他有关,但我已经在这方面停留了几天了

请注意,这是完美的&模拟器上没有任何错误

我哪里会出错

[更新]

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

[更新2]

我还使用了其他一些测试

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *err = nil;
NSArray *dirArr = [fileManager contentsOfDirectoryAtPath:documentsDir error:&err];
NSLog(@"~~Contents:\n%@",dirArr);
NSLog(@"~~Error: \n%@",err);
令人惊讶的是,它给出了
Cocoa错误256
,还说“不是目录”。这几乎就像
文档目录不存在一样。但根据
NSSearchPathForDirectoriesInDomains的说法,它确实存在`

这是我得到的输出

2011-10-30 10:59:16.934 xxxx[6774:707]~~内容:
(空)
2011-10-30 10:59:16.936 xxxx[6774:707]~~错误:
Error Domain=NSCOCAERRORDOMAIN Code=256“操作无法完成。(可可错误256)。”UserInfo=0x10052580{NSUserStringVariant=( 文件夹 ),NSFilePath=/var/mobile/Applications/FF654016-4257-47BB-99FE-55DB5453BBC6/Documents,NSUnderlyingError=0x10054a50“操作无法完成。不是目录”}

试着改变

[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
为了


并检查它是否不会返回
nil
,以确保除了在第一次浏览时对我来说看起来很好之外,不会出现任何问题。

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


我将iTunes Connect的捆绑ID用于此特定应用程序。现在一切正常。

您不应该发布fileManager,它是autoreleased@phix23,在文档中发布它,所以我认为这样做会更好。你不应该发布它,因为你没有分配它。什么是databasePath?这不应该是什么如果你不调用
alloc
new
copy
你不拥有它,因此不要释放它,那么请遵守内存管理规则it@Paul.s-我刚从Java来…我试过这个,还放了一个
if
检查其结果是否为
nil
,但事实并非如此。正常错误仍会发生。
[[NSBundle mainBundle] pathForResource:@"WaypointDatabase" ofType:@"sql"];