我正在使用核心数据并为我的一个实体设置一对多关系。我有两个实体。“团队”和“玩家”我正在尝试将 NSMutableSet 的玩家添加到团队中。
以下是我尝试将一名球员添加到球队中的方式。
-(void)addPlayerButton {
[_tempSet addObject:@""];
NSLog(@"number of cells in _tempSet is:%i",[_tempSet count]);
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
}
这就是我保存的方式
-(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];
}
有两件事出错了,一是 _tempSet 只添加了一个对象,不能再添加了。当我在 [self.team addPlayers: tempSet]行之前单击保存时,第二个崩溃;出现错误 [ _NSCFConstantString _isKindOfEntity:]: unrecognized selector sent to instance 0xd7cd8'
我对 Core Data 比较陌生,所以如果我做错了什么,请随时纠正我......