IOS - 来自NSBundle mainbundle的SQLite DB路径返回null

时间:2014-02-02 04:25:29

标签: ios sqlite resourcebundle

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"idataBase.sqlite3"];
NSLog(@"%@",databasePath);
NSString *databasePathFromApp = [[NSBundle mainBundle] pathForResource:@"idataBase" ofType:@"sqlite3"];
NSLog(@"%@",databasePathFromApp);
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];
if (!databaseAlreadyExists){
    [[NSFileManager defaultManager] copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}

我试图将数据库复制到文档目录,但是当我尝试从主bundle获取数据库路径并且我的应用程序崩溃时,它返回null。该数据库存在于应用程序中。但为什么它会返回null?

1 个答案:

答案 0 :(得分:2)

如果项目中有两次.sqlite3

因为可能是你的xCode目标是“Build Phases”在“Copy Bundle Resources”下添加文件。“