I have the following entities:
User. The common user entity with username, mail, etc.ComputerUserWithAI. Similar to user but it’s controlled by the application.Pick. Contains a relation toGameand it should hold a picker.
Since a Picker can be an User or a ComputerUserWithAI I created an Abstract Entity called Picker and I made User and ComputerWithAI extend that Picker class.
Once I added the Picker entity I made Pick have a relation to the Picker entity called picker.
Everything went fine, but I can’t insert a ComputerUserWithAI to a Pick. My code is like this:
ComputerUserWithAI *userWithAI = [NSEntityDescription insertNewObjectForEntityForName:@"ComputerUserWithAI" inManagedObjectContext:ctx];
userWithAI.name = @"DeepBlue";
Pick *pick = [NSEntityDescription insertNewObjectForEntityForName:@"Pick" inManagedObjectContext:ctx];
pick.game = game;
pick.picker = userWithAI;
The error I get is the following:
Terminating app due to uncaught exception
‘NSInvalidArgumentException’, reason: ‘Unacceptable type of value for
to-one relationship: property = “picker”; desired type = Picker; given
type = NSManagedObject; value = (entity:
ComputerUserWithAI…
But when I do:
pick.picker = [NSEntityDescription insertNewObjectForEntityForName:@"Picker" inManagedObjectContext:ctx];
it does work.
I checked the generated classes from core data and they are extending the Abstract Entity correctly. What might be wrong?
Found the error. The generated
Pick.hhad the following:Changing it to:
made everything work.
Try verifying the code generated by Xcode. This happened to me using Xcode 4.2.