【发布时间】:2014-09-21 20:50:42
【问题描述】:
我开发的 iPhone 应用程序曾一度陷入困境,
我想做的是,
我想将我的数据库从bundle 复制到folder inside document directory
当我尝试在DocDir 中创建文件夹时,它没有被创建并显示错误,
无法创建 MyDB.sql 文件并显示消息“操作无法完成。 (Cocoa 错误 4。)
这是我的代码 sn-p,
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [[paths objectAtIndex:0] stringByAppendingString:@"/NewFolder/"];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"MyDB.sql"];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[NSBundle mainBundle] pathForResource:@"MyDB" ofType:@"sql"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
我在哪里做错了?请帮忙
提前致谢。
【问题讨论】:
-
你用谷歌搜索过那个错误代码吗?
-
是的,但即使在尝试了我从谷歌获得的新代码后也面临同样的问题
-
错误代码。您是否搜索过 Cocoa 错误 4 的含义?
标签: ios nsbundle nsdocumentdirectory