gpt4 book ai didi

iphone - 将文件从包复制到文档不起作用(Cocoa 错误 512)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:10 26 4
gpt4 key购买 nike

我正在尝试简单地将我的 sqlite3 数据库复制到文档目录。我得到一个 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 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] 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"}

最佳答案

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

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

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

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