gpt4 book ai didi

objective-c - 找不到实体的 NSManagedObjectModel

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:05:07 27 4
gpt4 key购买 nike

这是 viewDidLoad 中 fetchRequest 的代码,该代码来自找到的教程 here只是我以编程方式链接导航 Controller 和 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];}

产品信息.h:

@class ProductDetails;

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

@end

获取结果 Controller

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

最佳答案

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

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

关于objective-c - 找不到实体的 NSManagedObjectModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6039660/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com