将文件从包复制到文档不工作(可可错误512) [英] Copying file from bundle to documents not working (Cocoa error 512)

查看:120
本文介绍了将文件从包复制到文档不工作(可可错误512)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想简单地将我的sqlite3数据库复制到documents目录。我得到一个 Cocoa Error 512 ,我想到的是,它不是一个有效的目录(或类似的东西。



数据库文件在XCode中的资源文件夹中(文件名是正确的)



这里是我想使用的代码:

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

// databasePath + databaseName标题
databaseName = @WaypointDatabase.sql;
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

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

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

NSLog(@Path in bundle:\\\
%@ \\\
\\\
,databasePathFromApp);
NSLog(@要复制到的路径:\\\
%@ \\\
\\\
,databasePath);

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

我得到的输出到控制台是: / p>


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]错误Domain = NSCocoaErrorDomain代码= 512操作无法完成(Cocoa错误512.)UserInfo = 0xe8b7cd0 {NSUserStringVariant =(
复制
),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操作无法完成。不是目录}



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 =错误域= NSPOSIXErrorDomain代码= 20 \操作无法完成不是目录\;
NSUserStringVariant =(
复制
);
}


我认为这必须与获取目录路径或某些东西有关,



注意,这样做在模拟器上没有任何错误。 b
$ b

我在哪里可能出错?



[UPDATE]

这可能是因为 Documents -folder目录不存在吗?



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

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

令人惊讶的是它给出了 Cocoa Error 256 也说不是目录。它几乎就像 Documents目录不存在。但是,根据 NSSearchPathForDirectoriesInDomains`



这是我得到的输出


2011-10-30 10:59:16.934 xxxx [6774:707] ~~内容:

(null)

2011-10-30 10:59:16.936 xxxx [6774:707] ~~错误:

错误Domain = NSCocoaErrorDomain Code = 256无法完成操作(Cocoa错误256.)UserInfo = 0x10052580 {NSUserStringVariant =(
文件夹
),NSFilePath = / var / mobile / Applications / FF654016-4257-47BB-99FE-55DB5453BBC6 / Documents,NSUnderlyingError = 0x10054a50操作无法完成。 }



解决方案

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



我使用iTunes Connect的Bundle ID用于此特定应用程式。现在一切工作完美。


I am trying to simply copy my sqlite3 database to the documents directory. I get a Cocoa Error 512 and what I figured about that is, that it's not a valid directory (or something like that.

The database file is in my Resources folder in XCode. (The name of file is correct)

Here is the code I am trying to use:

-(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]);
    }
}

And the output to console that I get is:

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 ); }

I figure this must be to do with getting the directory paths or something, but have been stuck on this for a few days now.

Take note, this works perfectly & without any errors on the simulator.

Where could I have gone wrong?

[UPDATE]

Could this be because the Documents-folder directory does not exist? How would I go about to create/check it?

[UPDATE 2]

I have done some other tests using

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

And surprisingly it gave Cocoa Error 256 and also said "Not a directory". It is almost as if the Documents directory does not exist. But it does, according toNSSearchPathForDirectoriesInDomains`

Here is the output I got

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"}

解决方案

The problem got solved by setting a non standard Bundle ID in die info.plist

I used the Bundle ID from iTunes Connect for this specific app. Now everything works perfectly.

这篇关于将文件从包复制到文档不工作(可可错误512)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆