Why should I set the parent entity with a subclass and an abstract object?

The problem I am facing is that I cannot assign an attribute to a subclass unless I set the property of the parent entity of the subclass of the entity of the abstract superclass.

In Xcode 4.0.2, this is the Parent Entity property that I refer to:

enter image description here

What I do not understand is that I thought that the parent entity was intended for the relationship between parents and children between objects, and the relations of objects are simply captured by class definitions.

Example

Objects A, B, and C:

  • A is an abstract object of type A with attributes:
    • y: string
    • z: string
  • B is an object of type A without attributes
  • C is an object of type A without attributes

Classes A, B and C:

@interface A : NSManagedObject { } @property (nonatomic, retain) NSString * y; @property (nonatomic, retain) NSString * z; @interface B : A { } @interface C : A { } 

Problem

If I do not set the parent entity for objects B and C as entity A, then when I try:

 NSEntityDescription *be = [[mom entitiesByName] objectForKey:@"B"]; B *b = [[NSManagedObject alloc] initWithEntity:be insertIntoManagedObjectContext:moc]; by = @"test"; // <<-- This line causes the following error: 

I get:

 -[NSManagedObject setY:]: unrecognized selector sent to instance 

If I set the parent object, it seems to work, except that the entity stored in the repository seems to be A instead of B.

+4
source share
1 answer

Don't remember to set the class for object B to class B? If so, you should take a look at the type of pointer that is assigned to b ... is it really B *? It looks like this is NSManagedObject * based on the error you are getting and according to the docs for -initWithEntity:insertIntoManagedObjectContext: ...

A dynamically generated subclass will be based on the specified class by the subject, so specifying a custom class in your model will replace the class passed for allocation.

... I think you should get B * if your model is configured correctly.

+3
source

All Articles