找不到实体名称的NSManagedObjectModel [英] could not locate an NSManagedObjectModel for entity name
问题描述
这是toggleAddProject方法的代码,Core Data代码几乎与Apple的CoreDataBooks示例中的代码相同,但是当我点击添加按钮时,应用程序崩溃, entityForName:找不到NSManagedObjectModel在
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];
}
一切都已合成,Project实体存在。我不知道为什么它崩溃。大多数人似乎能够通过在方法本身或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:
- 在实体名称中输入。
- Nil管理对象上下文对象。
- 无法将包含实体的模型添加到上下文使用的持久存储。
- 无法向上下文本身添加正确的永久存储。
- Typo in the Entity name.
- Nil managed object context object.
- Failure to add the model containing the entity to the persistent store the context uses.
- Failure to add the correct persistent store to the context itself.
这篇关于找不到实体名称的NSManagedObjectModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!