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 to- Gameand 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.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论