'+entityForName: nil 不是合法的 NSManagedObjectContext 参数 - 核心数据 [英] '+entityForName: nil is not a legal NSManagedObjectContext parameter - Core Data

查看:24
本文介绍了'+entityForName: nil 不是合法的 NSManagedObjectContext 参数 - 核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将所有相关代码添加到 App Delegate,并且我能够添加到数据模型并从 applicationDidFinishLaunchingWithOptions 中的数据模型获取.

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.

当我试图在我的视图控制器中写入数据模型时,我的问题就出现了.我已将此代码添加到头文件中:

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.

推荐答案

如果您使用 segues,如果您不将上下文向下传递,您将遇到同样的问题.在启动 segue 的类的 prepareForSegue 方法中使用此代码:

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:

[[segue destinationViewController] setManagedObjectContext:self.managedObjectContext];

当然,假设您将上下文保存在名为managedObjectContext"的属性中.

That assumes you hold your context in a property called "managedObjectContext" of course.

这篇关于'+entityForName: nil 不是合法的 NSManagedObjectContext 参数 - 核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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