Question

I'm saving the Managed Object Context, and am using the following to do it:

trainingDayObject = [NSEntityDescription entityForName:@"trainingDay" inManagedObjectContext:self.context];

It works, everything seems great, but I'm getting the warning:

Incompatible pointer types assigning to 'VitTrainingDay *' from 'NSEntityDescription *'

VitTrainingDay is an NSManagedObject Subclass of the Core Data entity TrainingDay. trainingDayObject is an instance of VitTrainingDay

I've tried reading the docs on NSEntityDescription, but since I'm assigning to an Entity, I'm confused about what the problem is.

I'm pretty new to core data and Objective-C, so forgive me if this is really obvious. It's been bothering me for a few days now.

Was it helpful?

Solution

When you do this:

[NSEntityDescription entityForName:@"trainingDay" inManagedObjectContext:self.context];

What you get is an instance of NSEntityDescription. This is an object that is equivalent to the entity type that you configured in the Core Data model editor in Xcode. It represents an entity type, not an instance of that entity.

From the error message it appears that trainingDayObject is declared as VitTrainingDay *, which is a pointer to an instance of a managed object.

The difference here is exactly the same idea as the difference between a class and an instance of a class. It's like you're trying to assign the NSString class to something that's supposed to be a specific instance of a string.

What you actually want is something like

trainingDayObject = [NSEntityDescription insertNewObjectForEntityForName:@"trainingDay" inManagedObjectContext:self.context];

Because this method creates a new instance of the entity type, instead of just giving you the entity type object itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top