今天在作一个数据库时遇到一个很奇葩的问题,导入一个db数据库,个人思路是这样的,由于若是要对数据库进行操做,须要先把这个数据库拷贝一份到沙盒中,可是当我用这个方法数据库
NSString *backupDbPath = [[NSBundle mainBundle] pathForResource:@"ICD10" ofType:@"db"];
时,却遇到很奇葩的问题,一直提示backupDbPath为nil,找了一上午快被折磨疯了,终于找到症结所在,原来是苹果捣的鬼,在拖入db文件到工程时,苹果默认把add Target前面的勾给去掉了!!!致使不管如何也找不到咱们导入的db文件,so,解决方法就是:code
把db文件从工程中删除,而后从新拖进去,当弹出窗口时,别着急着看都没看就点next,把add Target前面的勾勾上,而后就会发现,真的好了!图片
问题很简单,却困扰了我好久,特记录在此,须要勾上的不止copy item,还有add Target,警告本身时刻细心细心再细心,也但愿能帮到遇到一样问题的朋友。ip
图片好像没有显示出来,你们凑合着看吧。奉上个人一部分代码:get
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); NSString *doc = [searchPaths objectAtIndex:0]; NSString *dbFilePath = [doc stringByAppendingPathComponent:@"ICD10.db"]; NSFileManager *fm = [NSFileManager defaultManager]; BOOL isExist = [fm fileExistsAtPath:dbFilePath]; if (!isExist) { //拷贝数据库 //NSString *backupDbPath =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ICD10.db"]; NSString *backupDbPath = [[NSBundle mainBundle] pathForResource:@"ICD10" ofType:@"db"]; NSError *error = [[NSError alloc] init]; BOOL cp = [fm copyItemAtPath:backupDbPath toPath:dbFilePath error:&error]; if (cp) { NSLog(@"数据库拷贝成功"); }else{ NSLog(@"数据库拷贝失败: %@",[error localizedDescription]); } }