'+entityForName: nil is not a legal NSManagedO

2020-01-26 03:45发布

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.

8条回答
Ridiculous、
2楼-- · 2020-01-26 03:52

In my case the .xcdatamodeld was mislabeled in the AppDelegate:

 let container = NSPersistentContainer(name: "name of data model")
查看更多
一夜七次
3楼-- · 2020-01-26 03:55

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.

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;
    }
查看更多
太酷不给撩
4楼-- · 2020-01-26 04:04

you should add this to your viewController:

 id delegate = [[UIApplication sharedApplication] delegate];
    self.managedObjectContext = [delegate managedObjectContext];
查看更多
Emotional °昔
5楼-- · 2020-01-26 04:09

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];

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

查看更多
我只想做你的唯一
6楼-- · 2020-01-26 04:09

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];
查看更多
Juvenile、少年°
7楼-- · 2020-01-26 04:14

If the destination view controller is embedded in a NavigationController, the context needs to be set appropriately as follows-

  self.mydetailViewController = [[[segue destinationViewController] viewControllers] objectAtIndex:0];
 [self.mydetailViewController setManagedObjectContext:self.managedObjectContext];
查看更多
登录 后发表回答