データベースファイルをバンドルからユーザードキュメントにコピーしたい。
私のコードは以下の通りです:
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。)'。
提案してください、ありがとう。