ios '+entityForName: nil 不是合法的 NSManagedObjectContext 参数 - 核心数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11596487/
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
'+entityForName: nil is not a legal NSManagedObjectContext parameter - Core Data
提问by Alex Godbehere
I have added all of the relevant code to the App Delegate, and I am able to add to the data model and fetch from the data model in applicationDidFinishLaunchingWithOptions.
我已将所有相关代码添加到 App Delegate,并且我能够添加到数据模型并从 applicationDidFinishLaunchingWithOptions 中的数据模型中获取。
My problem comes when I am trying to write to the data model in my View Controller. I have added this code to the header file:
当我尝试写入视图控制器中的数据模型时,我的问题就出现了。我已将此代码添加到头文件中:
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
And this code to my implementation file:
并将此代码添加到我的实现文件中:
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *model = [NSEntityDescription
insertNewObjectForEntityForName:@"Events"
inManagedObjectContext:context];
[model setValue:@"Sample Event" forKey:@"eventName"];
NSError *error;
if (![context save:&error]) {
NSLog(@"Couldn't save: %@", [error localizedDescription]);
}
However, I get the following error:
但是,我收到以下错误:
'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Events''
Does anyone know what's going on? Any help would be appreciated.
有谁知道发生了什么?任何帮助,将不胜感激。
采纳答案by Tim
If you are using segues you will get the same problems if you don't pass the context down the line. Use this code in the prepareForSegue method of class initiating the segue:
如果您正在使用 segues,如果您不将上下文传递下去,您将遇到同样的问题。在启动 segue 的类的 prepareForSegue 方法中使用此代码:
[[segue destinationViewController] setManagedObjectContext:self.managedObjectContext];
That assumes you hold your context in a property called "managedObjectContext" of course.
当然,这假设您将上下文保存在名为“managedObjectContext”的属性中。
回答by Alex Godbehere
I had forgotten to pass the context to the view controller. Rookie error.
我忘记将上下文传递给视图控制器。菜鸟错误。
回答by Vishwani
You can pass the context by including the following code before you begin to fetch the data form the database:
在开始从数据库中获取数据之前,您可以通过包含以下代码来传递上下文:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
context = [appDelegate managedObjectContext];
回答by Hashem Aboonajmi
you should add this to your viewController:
你应该把它添加到你的 viewController 中:
id delegate = [[UIApplication sharedApplication] delegate];
self.managedObjectContext = [delegate managedObjectContext];
回答by rafaeljuzo
I got this problem and a colleague helped me out. If you got this error message: "entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name". And you made changes in you coredata model. I think the problem might not be the code.
我遇到了这个问题,一位同事帮助了我。如果您收到此错误消息:“entityForName: nil 不是搜索实体名称的合法 NSManagedObjectContext 参数”。您对核心数据模型进行了更改。我认为问题可能不是代码。
The solution can be simple. Try one of those options:
解决方案可以很简单。尝试以下选项之一:
- Just delete the app from the device you are testing, it should have the old version of your model.
- Create another database version using Xcode, >Editor>Add Model Version.
- 只需从您正在测试的设备中删除该应用程序,它应该具有您模型的旧版本。
- 使用 Xcode 创建另一个数据库版本,>Editor>Add Model Version。
Hope it helps.
希望能帮助到你。
回答by camelCaseUpInYourFace
In my case the .xcdatamodeld
was mislabeled in the AppDelegate:
在我的情况下.xcdatamodeld
,在 AppDelegate 中被错误标记:
let container = NSPersistentContainer(name: "name of data model")
回答by SpaceTrucker
I'm a fan of lazy initialization. This way if you need to inject a new context for testing you can, or it'll get it's context from the app delegate if you set up your MOC there.
我是懒惰初始化的粉丝。这样,如果您需要注入一个新的上下文进行测试,您可以,或者如果您在那里设置 MOC,它将从应用程序委托中获取它的上下文。
class.h
@property (strong, nonatomic,getter=getManagedObjectContext) NSManagedObjectContext *managedObjectContext;
class.m
-(NSManagedObjectContext *)getManagedObjectContext {
if (_managedObjectContext) {
return _managedObjectContext;
}
_managedObjectContext = [[(AppDelegate *)[[UIApplication sharedApplication]delegate]sharedDataModel]managedObjectContext];
return _managedObjectContext;
}
回答by hackerinheels
If the destination view controller is embedded in a NavigationController, the context needs to be set appropriately as follows-
如果目标视图控制器嵌入在 NavigationController 中,则需要按如下方式适当设置上下文 -
self.mydetailViewController = [[[segue destinationViewController] viewControllers] objectAtIndex:0];
[self.mydetailViewController setManagedObjectContext:self.managedObjectContext];