mathisonian
mathisonian

Reputation: 1420

CoreData relationship with subclass

I have the following coredata entities:

Event, User, SpecialUser, such that SpecialUser is a subclass of User, and there is a relationship Event.owner to User

I am running into a situation where I want to say

event.owner = specialUser, but this throws the error

'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-one relationship: property = "owner"; desired type = User; given type = SpecialUser;

I have also tried casting during the assignment, e.g. event.owner = (User *) specialUser, to no avail.

Upvotes: 3

Views: 424

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70936

It looks like your Event entity type has an owner relationship that points to the User entity type. If that's the case, you can only ever assign a User to that relationship.

The fact that the SpecialUser class subclasses the User class is not relevant to Core Data. What it cares about are the entity types. Class inheritance is meaningless if there's no corresponding entity inheritance. Basically, your class inheritance and entity inheritance should match.

Make the SpecialUser entity type inherit from the User entity type, to parallel the class hierarchy. Then you can assign a SpecialUser as the owner, and it will be valid for Core Data.

Upvotes: 3

Related Questions