无法找到实体的NSManagedObjectModel [英] Could not locate NSManagedObjectModel for Entity

查看:61
本文介绍了无法找到实体的NSManagedObjectModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是viewDidLoad中fetchRequest的代码,并且该代码是从此处,只是我以编程方式链接了导航控制器和表格视图,而不是使用界面生成器。实体ProductInfo存在。但是,当我运行程序时,出现错误:

The is the code for the fetchRequest in viewDidLoad and the code is followed from a tutorial found here just that I'm linking the navigation controller and the tableview programmatically instead of using interface builder. The entity ProductInfo exists. However when I run the program I get the error:

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

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

I have reset the simulator incase it was an old model but the error still occurs. I have also switched to use a FetchedResultsController but the problem still persists. Is the problem because these fetchedResultsController methods aren't inside the appdelegate? They are currently in a TableViewController. How can I solve this problem?

viewDidLoad方法:

viewDidLoad Method:

- (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:

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

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;

}

任何帮助都值得赞赏。

Any help much appreciated.Thanks in advance.

如果我粘贴的上述片段无济于事,我也会在整个项目中附加数据模型。

In case the above fragments i pasted did not help, I attached the whole project with the data model inside too.

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

推荐答案

以前曾经遇到过此问题,听起来好像是xcdatamodeld文件中的实体名称的简单拼写错误。字符串 ProductInfo必须与模型文件中实体的名称完全匹配。

Having run into this problem before, it sounds like it might be a simple misspelling of the entity name in the xcdatamodeld file. The string "ProductInfo" must match exactly the name of the entity in the model file.

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

It might also be that your context is not making the correct reference. Consider showing some more of your code related to the context if the above doesn't fix the issue.

这篇关于无法找到实体的NSManagedObjectModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆