我想将数据库文件从捆绑包复制到用户文档。
我的代码如下:
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.)'。
请建议,谢谢。