質問

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];
}

I tried to copy the database to document directory, But it returns null when i try to get the database path from main bundle and my app crashed. The database is present in app. But why it returns null?

役に立ちましたか?

解決

if you have twice .sqlite3 in your project

because may be your xCode target is "Build Phases" add the file under "Copy Bundle Resources"".

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top