3

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.

Arel
  • 3,888
  • 6
  • 37
  • 91

1 Answers1

4

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.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • If I use `insertNewObjectForEntityForName` I end up creating blank objects in addition to saving the MOC from the other created objects. For example, in my table view I get nil, nil, Name 1, Name 2, instead of the expected Name1, Name 2. – Arel Dec 16 '13 at 17:48
  • Then ask another question that provides more detail on what you're doing. For example, what you're trying to do when you use the line of code in your question above. With only the one line, it's impossible to give an answer that reflects context of the overall structure of your app. What's possible, which I did, is to explain what's wrong with that one line. – Tom Harrington Dec 16 '13 at 17:59
  • I thought it was a simpler problem, and didn't want to overly complicate the question. Here is my other question with context: http://stackoverflow.com/questions/20589938/saving-the-managedobjectcontext-for-a-specific-object – Arel Dec 16 '13 at 18:18