0

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...

Luke
  • 612
  • 1
  • 6
  • 19

1 Answers1

0

In the first function you have the line

[_tempSet addObject:@""];

then later:

[self.team addPlayers:_tempSet];

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:

Player *newPlayer = (Player *)[NSEntityDescription insertNewObjectForEntityForName:@"Player" 
inManagedObjectContext:managedObjectContext];
[_tempSet addObject:newPlayer];

But you probably also want to set some attributes for the player, not just add an empty player to the team.

Jesse Crocker
  • 863
  • 5
  • 14
  • Hey thank you for the reply, that worked awesome... So yeah there are three attributes I need to add to the player that are coming from a custom cell. first name, last name, and number... So In my add player function, can I just say cell.firstNameText.text = newPlayer.firstName etc? – Luke Jul 04 '12 at 18:48
  • Yeah, newPlayer.firstName = cell.FirstNameText.text . Also, remember to accept correct answers. – Jesse Crocker Jul 04 '12 at 19:55