问题描述
这是 toggleAddProject 方法的代码,Core Data 代码几乎与 Apple 的 CoreDataBooks 示例中的代码相同,但是当我单击添加按钮时,应用程序崩溃了 entityForName: could not locate an NSManagedObjectModel for entity name'Project'
在以 newProjectController.project
This is the code for toggleAddProject method, the Core Data code is almost the same as found in Apple's CoreDataBooks sample, however when I click the add button the app crashes with entityForName: could not locate an NSManagedObjectModel for entity name 'Project'
on the line starting with newProjectController.project
-(IBAction)toggleAddProject
{
NewProjectViewController *newProjectController = [[[NewProjectViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
// Create a new managed object context for the new project -- set its persistent store coordinator to the same as that from the fetched results controller's context.
NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] init];
self.addingManagedObjectContext = addingContext;
[addingManagedObjectContext setPersistentStoreCoordinator:[[fetchedResultsController managedObjectContext] persistentStoreCoordinator]];
newProjectController.project = (Project *)[NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:addingContext];
[addingContext release];
UINavigationController *addNewNavigationController = [[UINavigationController alloc] initWithRootViewController:newProjectController];
[self.navigationController presentModalViewController:addNewNavigationController animated:YES];
[addNewNavigationController release];
}
一切都已合成,项目实体存在.我无法弄清楚它为什么会崩溃.大多数人似乎能够通过在方法本身或 viewDidLoad 中插入以下代码来修复此错误:
Everything has been synthesized, the Project entity exists. I can't figure out why it crashes. Most people seem to be able to fix this error by inserting the following code either in the method itself, or in viewDidLoad:
if (managedObjectContext == nil)
{
managedObjectContext = [(CoreDataBooksAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
为我的应用程序委托修改后,它没有任何区别.感谢您的帮助.
When modified for my app delegate it makes no difference. Thanks for any help.
推荐答案
这个错误只有几个可能的来源:
This error has only a few possible sources:
- 实体名称中的拼写错误.
- 无管理对象上下文对象.
- 未能将包含实体的模型添加到上下文使用的持久存储中.
- 未能将正确的持久存储添加到上下文本身.
这篇关于找不到实体名称的 NSManagedObjectModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!