I am using core data and set up a one to many relationship for one of my entities. I have two entities. “Team” and “Player” I am trying to add an NSMutableSet of players to the team.
Below is how I am attempting to add a player to the team.
-(void)addPlayerButton {
[_tempSet addObject:@""];
NSLog(@"number of cells in _tempSet is:%i",[_tempSet count]);
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
}
This is how I am saving
-(void)saveButtonWasPressed {
self.team =[NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:self.managedObjectContext];
self.player = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:self.managedObjectContext];
[team addPlayersObject:player];
team.schoolName = _schoolName.text;
team.teamName = _teamName.text;
team.teamID = _teamName.text;
team.season = _season.text;
team.headCoach = _headCoach.text;
team.astCoach = _assistantCoach.text;
player.firstName = cell.playerFirstName.text;
player.lastName = cell.playerLastName.text;
player.number = cell.playerNumber.text;
[self.team addPlayers:_tempSet];
[self.managedObjectContext save:nil];
[self.navigationController popViewControllerAnimated:YES];
}
There are two things going wrong, one, the _tempSet only adds one object and can not add anymore. and the second crashes when I click save right before the line [self.team addPlayers:tempSet]; With the error [_NSCFConstantString _isKindOfEntity:]: unrecognized selector sent to instance 0xd7cd8′
I am relatively new to Core Data so please feel free to correct me if I am doing something else wrong…
In the first function you have the line
then later:
So your adding an NSString to the players relationship of team, when you need to be adding a Player entity.
In the first function try something like:
But you probably also want to set some attributes for the player, not just add an empty player to the team.