这是toggleAddProject方法的代码,核心数据代码几乎与在苹果的CoreDataBooks示例中找到的代码相同,但是当我单击添加按钮时,应用程序崩溃,entityForName: could not locate an NSManagedObjectModel for entity name 'Project'在以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中插入以下代码来修复此错误:
if (managedObjectContext == nil)
{
managedObjectContext = [(CoreDataBooksAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}当为我的应用程序代理修改时,这没有什么不同。谢谢你的帮助。
发布于 2010-07-29 03:40:44
此错误只有几个可能的来源:
实体名称中的
发布于 2011-09-03 01:53:17
当我有几个不同的NSManagedObjectContext时,我遇到了这个问题。调试它的快速方法是检查不同的连接位,并确保在调用上下文之前列出我的实体。
NSLog(@"Context: %@",context);
NSLog(@"PS Coord : %@",context.persistentStoreCoordinator);
NSLog(@"MOM : %@", context.persistentStoreCoordinator.managedObjectModel);
NSLog(@"Entities : %@",[[context.persistentStoreCoordinator.managedObjectModel entities] valueForKey:@"name"]); 发布于 2010-07-28 09:13:01
使用调试器并确认您的模型不为空。这是导致此错误的最常见原因。如果不是nil,则查找实体名称中的拼写错误。
https://stackoverflow.com/questions/3348333
复制相似问题