Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Core Data error - Unacceptable type of value for to-many relationship

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.

Core Data Model

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!

like image 448
aritchie Avatar asked Apr 18 '13 11:04

aritchie


1 Answers

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;
like image 60
Martin R Avatar answered Oct 26 '22 14:10

Martin R