Error when inserting entities which extend an Abstract Entity
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 toGame
and 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 =开发者_StackOverflow 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.h
had the following:
@class Picker
@interface Pick : NSManagedObject
@property (nonatomic, retain) NSManagedObject *game;
@property (nonatomic, retain) Picker *picker;
Changing it to:
@interface Pick : NSManagedObject
@property (nonatomic, retain) NSManagedObject *game;
@property (nonatomic, retain) NSManagedObject *picker;
made everything work.
Try verifying the code generated by Xcode. This happened to me using Xcode 4.2.
精彩评论