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

标签 iphone file-manager

我想将数据库文件从 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/

相关文章:

iphone - iOS 11 自定义后退按钮问题

ios - iOS 7 分隔图像中的 UISegmentedControl 在动画期间是错误的

Android - 如何以编程方式启动设备 "file system"?

iphone - 修改文件 IOS 的 UTF 时间

asp.net - 用于 ASP.NET 的 KCFinder

android - 在文件管理器中打开特定文件夹进行查看

iphone - 如何使单元格在选择时透明

iphone - dispatch_async 被触发 10 次

iphone - iPhone 的颠倒界面

android - 在没有安装任何文件管理器的情况下在 Android 中选择一个文件