xcode CoreData 错误:+entityForName:找不到实体名称的 NSManagedObjectModel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7100302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:45:14  来源:igfitidea点击:

CoreData error: +entityForName: could not locate an NSManagedObjectModel for entity name

objective-cxcodecore-data

提问by marsalal1014

I've been struggling with CoreDatafor a few days, but I keep getting this error:

我已经挣扎CoreData了几天,但我不断收到此错误:

'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name.

'NSInternalInconsistencyException',原因:'+entityForName:找不到实体名称的 NSManagedObjectModel。

I have checked the entity name and what I wrote on my code and they're the same. I also recreated the object data-model and even delete the app from the simulator but nothing seems to fix it. Here's what I have:

我检查了实体名称和我在代码上写的内容,它们是相同的。我还重新创建了对象数据模型,甚至从模拟器中删除了应用程序,但似乎没有任何解决方法。这是我所拥有的:

method to save into CoreData:

保存到 CoreData 的方法:

-(IBAction)save:(id)sender {
    NSManagedObject * newNews = [NSEntityDescription insertNewObjectForEntityForName:@"NewsStand"
    inManagedObjectContext:coredata.managedObjectContext];
    [newNews setValue:news_title forKey:@"story_title"];
    [newNews setValue:news_desc forKey:@"story_desc"];
    [newNews setValue:news_image  forKey:@"story_image"];
    [newNews setValue:test  forKey:@"story_url"];
    [coredata commit];
    NSLog(@"data saved!!!!");
}

I have implemented all methods of core data in a separated class:

我已经在一个单独的类中实现了核心数据的所有方法:

applicationDocumentsDirectory,  
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator, 
- (NSManagedObjectModel *)managedObjectModel, 
- (NSManagedObjectContext *)managedObjectContext

回答by TechZen

This is a fairly common error and it has three causes:

这是一个相当常见的错误,它有三个原因:

  1. Misspelling the entity name e.g. NewsStandinstead of NewsStands.
  2. Having a nil managed object context
  3. Having no or the wrong managed object model loaded.
  1. 拼错实体名称,例如,NewsStand而不是NewsStands
  2. 有一个 nil 托管对象上下文
  3. 没有加载或加载了错误的托管对象模型。

(1) is the most common but (3) is more common than (2). You can check that you are loading the right model with the keypath:

(1) 是最常见的,但 (3) 比 (2) 更常见。您可以使用密钥路径检查您是否正在加载正确的模型:

aManagedObjectContext.persistentStoreCoordinator.managedObjectModel.entities

then check the entity's names.

然后检查实体的名称。

回答by pbergson

I had a similar problem and found TechZen's answer to be helpful (especially the suggestion to check the entities). However, my problem turned out to be a variant of (2): I could see that the moc itself wasn't nil, but I hadn't set the persistent store coordinator.

我遇到了类似的问题,发现 TechZen 的回答很有帮助(尤其是检查实体的建议)。但是,我的问题是 (2) 的一个变体:我可以看到 moc 本身不是 nil,但是我没有设置持久存储协调器。

[aManagedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator]

or similar.

或类似。

I'd add this as a comment to TechZen's answer, but can't yet, and want to include it in case someone else has my problem.

我会将此添加为 TechZen 答案的评论,但还不能,并希望将其包含在内,以防其他人遇到我的问题。

回答by Luc Bloom

During my development, I could not find Entities that I added later on. What worked for me:

在我的开发过程中,我找不到后来添加的实体。什么对我有用:

Uninstall the App EVERY TIME you change the Data Model!

每次更改数据模型时都卸载应用程序!

The Data Model is cached by Core Data between installs, to make sure the integrity stays in tact. Delete the App from the simulator / iPhone to be able to test your changes.

数据模型在安装之间由 Core Data 缓存,以确保完整性保持不变。从模拟器/iPhone 中删除应用程序,以便能够测试您的更改。

PS: does anyone know how to do that automatically?

PS:有谁知道如何自动做到这一点?

回答by Mark Adams

Ensure that coredata.managedObjectContextis not nil.

确保coredata.managedObjectContext不为零。

回答by tnd

Don't have the rep to comment - but Luc Bloom's response fixed MYissue. I completely forgot I changed some stuff in the data model after initial build/install and spent entirely too long banging my head against the desk.

没有代表发表评论 - 但 Luc Bloom 的回应解决了我的问题。我完全忘记了在初始构建/安装后我更改了数据模型中的一些内容,并且花了很长时间将我的头撞在桌子上。

回答by Jeff

If you're editing a framework and running a unit test to get the error, make sure your xcdatamodeldfile is added to the test target. Frameworks behave differently than normal projects.

如果您正在编辑框架并运行单元测试以获取错误,请确保您的xcdatamodeld文件已添加到测试目标。框架的行为与普通项目不同。