无法find实体的NSManagedObjectModel

这是在viewDidLoad中的fetchRequest的代码,从这里find的教程后面的代码只是我编程链接导航控制器和tableview,而不是使用接口生成器。 实体ProductInfo存在。 但是,当我运行该程序时,我得到的错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'ProductInfo'' 

我已经重置模拟器,因为它是一个旧的模型,但仍然发生错误。 我也切换到使用FetchedResultsController但问题仍然存在。 问题是因为这些fetchedResultsController方法不在appdelegate内部吗? 他们目前在一个TableViewController。 我怎么解决这个问题?

viewDidLoad方法:

 - (void)viewDidLoad{ NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription * entity = [NSEntityDescription entityForName:@"ProductInfo" inManagedObjectContext:context]; [fetchRequest setEntity:entity]; NSError * error; self.productInfos = [_context executeFetchRequest:fetchRequest error:&error]; [fetchRequest release]; [super viewDidLoad];} 

ProductInfo.h:

 @class ProductDetails; @interface ProductInfo : NSManagedObject { @private } @property (nonatomic, retain) NSString * productName; @property (nonatomic, retain) NSString * productPrice; @property (nonatomic, retain) ProductDetails * details; @end 

FetchedResultsController

 -(NSFetchedResultsController *)fetchedResultsController { if (_fetchedResultsController != nil) { return _fetchedResultsController; } NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription * entity = [NSEntityDescription entityForName:@"ProductInfo" inManagedObjectContext:_context]; //line that is causing the problem [fetchRequest setEntity:entity]; NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"productInfos.productName" ascending:YES]; [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; [fetchRequest setFetchBatchSize:20]; NSFetchedResultsController * theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext: _context sectionNameKeyPath:nil cacheName:@"Root"]; self.fetchedResultsController = theFetchedResultsController; _fetchedResultsController.delegate = nil; [sort release]; [fetchRequest release]; [theFetchedResultsController release]; return _fetchedResultsController; 

}

任何帮助非常感激。提前感谢。

如果我粘贴的上述碎片没有帮助,我也将整个项目与数据模型附加在一起。

http://www.mediafire.com/?5cns4q0sv9hqn6s

之前遇到这个问题,这听起来像它可能是在xcdatamodeld文件中的实体名称的简单拼写错误。 string“ProductInfo”必须完全匹配模型文件中实体的名称。

也可能是你的背景没有做出正确的参考。 如果上述问题没有解决问题,考虑展示一些与上下文相关的代码。

这发生在我每次开关电脑的时候。 我必须做的是

  1. 从模拟器卸载应用程序。
  2. 重命名存储UIManagedDocument的数据库文件。
  3. 执行构build清理。
  4. 构build并运行应用程序。