iOS – 复制sqlite捆绑为CoreData

以下是我所做的:创build与应用程序的填充版本的SQLite文件。 将该文件复制到我的应用程序包中,确保目标已设置,并且存在于“复制包资源”

然后我尝试像这样填充核心数据:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (_persistentStoreCoordinator != nil) { return _persistentStoreCoordinator; } NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TestModel.sqlite"]; if( ![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]] ) { // If there's no Data Store present (which is the case when the app first launches), identify the sqlite file we added in the Bundle Resources, copy it into the Documents directory, and make it the Data Store. NSString *sqlitePath = [[NSBundle mainBundle] pathForResource:@"TestModel" ofType:@"sqlite" inDirectory:nil]; NSError *anyError = nil; BOOL success = [[NSFileManager defaultManager] copyItemAtPath:sqlitePath toPath:[storeURL path] error:&anyError]; if(success){ NSLog(@"sucess, loading into store"); NSError *error = nil; _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); }else{ NSLog(@"error with sqlite file"); } } } return _persistentStoreCoordinator; 

}

但是我得到这个错误:

 NSFileManager copyItemAtPath:toPath:error:]: source path is nil 

我究竟做错了什么? 它是说,它无法find捆绑中的sqlite文件?

我认为这是因为当我从Finder导入到Xcode文件,它被命名为“TestModel”,而不是“TestModel.sqlite”。

从文档

bundlePath – 顶级捆绑目录的path。 这必须是一个有效的path。 例如,要指定Mac应用程序的捆绑软件目录,可以指定path/​​Applications/MyApp.app。

*编辑:我想我得到了错误的方法:(仍然尝试这一个虽然! – >为什么不使用- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension ?跳过目录反正也不想要)。