gpt4 book ai didi

iphone - 无法使用 copyItemAtPath 复制数据库文件,出现 cocoa 错误 4

转载 作者:行者123 更新时间:2023-12-03 21:03:08 25 4
gpt4 key购买 nike

我想将数据库文件从 bundle 复制到用户文档。

我的代码如下:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *userPath = [documentsDir stringByAppendingPathComponent:@"db.sql"];

NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sql"];

NSFileManager *fileManager = [NSFileManager defaultManager];

NSLog(@"Bundle database exists: %i",[fileManager fileExistsAtPath:srcPath]);
NSLog(@"User Document folder database exists: %i",[fileManager fileExistsAtPath:userPath]);


BOOL find = [fileManager fileExistsAtPath:userPath];
BOOL copySuccess = FALSE;

NSError *error;

if (!find) {
NSLog(@"don't have writable copy, need to create one");
copySuccess = [fileManager copyItemAtPath:srcPath toPath:userPath error:&error];
}

if (!copySuccess) {

NSLog(@"Failed with message: '%@'.",[error localizedDescription]);
}

结果总是说:

Bundle database exists: 1 User Document folder database exists: 0
don't have writable copy, need to create one Failed with message: 'The
operation couldn’t be completed. (Cocoa error 4.)'.

请推荐,谢谢。

最佳答案

用于确定用户文档目录的代码不正确。

使用您的代码,我整理了一个快速但有效的示例。对于您的应用程序,您可能希望创建一些包含静态函数“applicationDocumentsDirectory”的 utils 类,以便项目中的其他类可以在需要时调用它。

头文件:

#import <UIKit/UIKit.h>

@interface TST_ViewController : UIViewController

+ (NSString *) applicationDocumentsDirectory;

@end

实现文件:

#import "TST_ViewController.h"

@interface TST_ViewController ()

@end

@implementation TST_ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *dbFilename = @"db.sql";
NSString *userPath = [[TST_ViewController applicationDocumentsDirectory] stringByAppendingPathComponent:dbFilename];
NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbFilename];
NSFileManager *fileManager = [NSFileManager defaultManager];

BOOL foundInBundle = [fileManager fileExistsAtPath:srcPath];
BOOL foundInUserPath = [fileManager fileExistsAtPath:userPath];
BOOL copySuccess = FALSE;

NSError *error;

if(foundInBundle) {

if (!foundInUserPath) {
NSLog(@"Don't have a writable copy, so need to create one...");
copySuccess = [fileManager copyItemAtPath:srcPath toPath:userPath error:&error];
}

if (!copySuccess) {
NSLog(@"Failed with message: '%@'.",[error localizedDescription]);
}

} else {
// handle error in the event the file is not included in the bundle
}

}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

+ (NSString *) applicationDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}

@end

关于iphone - 无法使用 copyItemAtPath 复制数据库文件,出现 cocoa 错误 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12344377/

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