views:

10

answers:

1

I have several subclasses of NSManagedObject. They are all instantiated with code something like this:

    MeasurementDescriptor *descriptor = (MeasurementDescriptor *)[NSEntityDescription 
                                            insertNewObjectForEntityForName:@"MeasurementDescriptor"
                                            inManagedObjectContext:context];

or like this:

    Experiment *experiment = (Experiment *)[NSEntityDescription 
                                            insertNewObjectForEntityForName:@"Experiment"
                                            inManagedObjectContext:context];

What is odd, though, is that (from code above)

NSLog(@" descriptor's class = %@", NSStringFromClass([descriptor class]));

prints out 'NSManagedObject', while

NSLog(@" experiment's class = %@", NSStringFromClass([experiment class]));

prints out 'Experiment'.

Does anyone know why this would be? MeasurementDescriptor, unlike my other NSManagedObject subclasses, had no ivars (not including its Core Data properties). Adding an ivar did not change anything. Similarly, MeasurementDescriptor is the only NSManagedObject subclass without 'relationship' properties. Perhaps this accounts for this strangeness...???

+1  A: 

The only explaination is that your MeasurementDescriptor subclass is not actually known to the code. The most common causes of this are:

  1. In the data model editor not setting the Class attribute of the entity to the correct class.
  2. Not adding the source file for the subclass to the target.

This is easy to do with Core Data because if it can't find a dedicated subclass it doesn't complain but just returns a generic NSManagedObject initialized with the entity's property key names.

TechZen
@TechZen I agree with your explanation, but neither of the cases you listed are true for my example. The Entity Class for MeasurementDescriptor is MeasurementDescriptor, and the source file is included in the project. In fact, that's where the NSLog line is that alerted me to this issue. It may be that during migration from model A (in which there is no MeasurementDescriptor) to model B (in which MeasurementDescriptor in introduced) entities that exist in only one model are treated as NSManagedObjects. Though I am not sure why this would be.
westsider