【发布时间】:2011-03-21 20:55:06
【问题描述】:
这是 toggleAddProject 方法的代码,Core Data 代码与 Apple 的 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];
}
一切都已合成,项目实体存在。我无法弄清楚它为什么会崩溃。大多数人似乎都可以通过在方法本身或 viewDidLoad 中插入以下代码来修复此错误:
if (managedObjectContext == nil)
{
managedObjectContext = [(CoreDataBooksAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
当为我的应用委托进行修改时,它没有任何区别。感谢您的帮助。
【问题讨论】:
标签: iphone cocoa-touch core-data