CoreData错误:+ entityForName:找不到实体名称的NSManagedObjectModel [英] CoreData error: +entityForName: could not locate an NSManagedObjectModel for entity name

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

问题描述

我一直在努力与 CoreData 几天,但我一直收到这个错误:

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


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

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

我已经检查了实体名称以及我在代码上写了什么,他们是一样的。我也重新创建对象数据模型,甚至从模拟器中删除应用程序,但没有什么似乎解决它。
这是我有:

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:

方法保存到CoreData中:

method to save into 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


推荐答案

它有三个原因:


  1. 拼写实体名称例如 NewsStand 而不是 NewsStands

  2. li>
  3. 加载了没有或错误的托管对象模型。

  1. Misspelling the entity name e.g. NewsStand instead of NewsStands.
  2. Having a nil managed object context
  3. Having no or the wrong managed object model loaded.

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

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

aManagedObjectContext.persistentStoreCoordinator.managedObjectModel.entities

然后检查实体的名称。

then check the entity's names.

这篇关于CoreData错误:+ entityForName:找不到实体名称的NSManagedObjectModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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