Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
800 views
in Technique[技术] by (71.8m points)

objective c - '+entityForName: nil is not a legal NSManagedObjectContext parameter - Core Data

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had forgotten to pass the context to the view controller. Rookie error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...