这是我所做的:使用填充版本的应用程序创建了 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
我究竟做错了什么?是说在捆绑包中找不到sqlite文件吗?