2

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

我的代码如下:

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

结果总是说:

捆绑数据库存在:1 用户文档文件夹数据库存在:0
没有可写副本,需要创建一个失败并显示消息:'
操作无法完成。(可可错误 4.)'。

请建议,谢谢。

4

1 回答 1

2

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

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

头文件:

#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
于 2012-09-10T02:07:07.597 回答