I'm tearing my hair out with this issue, which seems like it should be something really simple. iOS / Objective C is new to me, so maybe I'm just not grasping something fundamental.
The problem is that I've added a new entity to my Core Data model, and set up a one-to-many relationship. The model already had two entities that had a one-to-one relationship.
The Players entity is the new one.
I have a UITableViewController where I'm saving to the attributes from UITextFields. This worked fine for the original configuration of Teams/TeamDetails, but it crashes out with the following error when I add in the Players entity to the code:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Unacceptable type of value for to-many relationship:
property = "playerDetails"; desired type = NSSet; given type = Players;
So, in my Save method, I had:
Teams *team = [NSEntityDescription insertNewObjectForEntityForName:@"Teams" inManagedObjectContext:self.managedObjectContext];
TeamDetails *teamdetails = [NSEntityDescription insertNewObjectForEntityForName:@"TeamDetails" inManagedObjectContext:self.managedObjectContext];
team.teamDetails = teamdetails;
then I go on to set the attributes like:
team.teamName = teamNameTextField.text;
team.teamDetails.managerName = managerNameTextField.text;
Which all worked no problem. Now I extend this to incorporate the Players Entity:
Players *playerDetails = [NSEntityDescription insertNewObjectForEntityForName:@"Players" inManagedObjectContext:self.managedObjectContext];
team.playerDetails = playerDetails;
And I get the error above. I've tried the following:
[team setPlayerDetails:playerDetails];
But this doesn't make any difference. I also tried:
NSSet *playerDetails = [NSEntityDescription insertNewObjectForEntityForName:@"Players" inManagedObjectContext:self.managedObjectContext];
But again, no difference - it still thinks it's getting an object of type Players rather than NSSet.
I feel like I'm just not grasping something simple, so any help would ge very much appreciated!
playerDetails
is a to-many relationship, so the value of team.playerDetails
is a set of players, not a single player object. You can either use
[team addPlayerDetailsObject:playerDetails];
or more simply, using the inverse relationship:
playerDetails.team = team;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With