首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到实体的NSManagedObjectModel

找不到实体的NSManagedObjectModel
EN

Stack Overflow用户
提问于 2011-05-18 11:50:13
回答 2查看 5.4K关注 0票数 2

这是viewDidLoad中fetchRequest的代码,该代码来自here的一个教程,我只是通过编程方式链接导航控制器和tableview,而不是使用接口构建器。实体ProductInfo存在。然而,当我运行这个程序时,我得到了错误:

代码语言:javascript
复制
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'ProductInfo''

我已经重置了模拟器,以防它是一个旧的模型,但错误仍然发生。我也改用了FetchedResultsController,但问题仍然存在。问题是因为这些fetchedResultsController方法不在appdelegate中吗?它们目前在TableViewController中。我该如何解决这个问题?

viewDidLoad方法:

代码语言:javascript
复制
- (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:

代码语言:javascript
复制
@class ProductDetails;

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

@end

FetchedResultsController

代码语言:javascript
复制
-(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;

}

任何有帮助的appreciated.Thanks都会提前。

如果我粘贴的上面的片段没有帮助,我也附加了整个项目和数据模型。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-18 12:02:37

以前遇到过这个问题,听起来可能是xcdatamodeld文件中实体名称的简单拼写错误。字符串"ProductInfo“必须与模型文件中实体的名称完全匹配。

也可能是您的上下文没有进行正确的引用。如果上述方法不能解决问题,请考虑显示更多与上下文相关的代码。

票数 3
EN

Stack Overflow用户

发布于 2012-04-16 23:09:36

每次我切换开发计算机时都会发生这种情况。我要做的是

从存储UIManagedDocument.

  • Perform a Build Clean的simulator.

  • Rename数据库文件卸载
  1. 应用程序。
  2. 构建并运行应用程序。
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6039660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档