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'を含む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
于 2012-09-10T02:07:07.597 に答える