iOS Core Data错误 - 一对多关系的不可接受的值类型 [英] iOS Core Data error - Unacceptable type of value for to-many relationship
问题描述
我在这个问题上撕掉了我的头发,这似乎应该是很简单的东西。 iOS / Objective C对我来说是新的,所以也许我只是没有抓住基本的东西。
问题是我已经添加了一个新的实体到我的核心数据模型,并设置一对多的关系。模型已经有两个具有一对一关系的实体。
玩家实体是新的。
我有一个UITableViewController,我从UITextFields保存到属性。这对于Teams / TeamDetails的原始配置工作正常,但是当我将Players实体添加到代码时,会出现以下错误:
由于未捕获异常而终止应用程序'NSInvalidArgumentException',
原因:'多对多关系的不可接受的值类型:
property =playerDetails;所需类型= NSSet;给定类型=玩家;
所以,在我的保存方法中,我有:
Teams * team = [NSEntityDescription insertNewObjectForEntityForName:@TeamsinManagedObjectContext:self.managedObjectContext];
TeamDetails * teamdetails = [NSEntityDescription insertNewObjectForEntityForName:@TeamDetailsinManagedObjectContext:self.managedObjectContext];
team.teamDetails = teamdetails;
然后我继续设置属性:
team.teamName = teamNameTextField.text;
team.teamDetails.managerName = managerNameTextField.text;
这些都没有问题。现在我扩展到包含玩家实体:
玩家* playerDetails = [NSEntityDescription insertNewObjectForEntityForName:@PlayersinManagedObjectContext:self。 managedObjectContext];
team.playerDetails = playerDetails;
我得到上面的错误。我试过以下:
[team setPlayerDetails:playerDetails];
但这没有什么区别。我也尝试过:
NSSet * playerDetails = [NSEntityDescription insertNewObjectForEntityForName:@PlayersinManagedObjectContext:self.managedObjectContext];
但是,没有什么区别 - 它仍然认为它是一个类型Players的对象,而不是NSSet。 / p>
我觉得我只是没有抓住一些简单的东西,所以任何帮助将非常赞赏!
playerDetails
是一个多对多关系,因此 team.playerDetails
是一个设置的玩家,而不是单个玩家对象。您可以使用
[team addPlayerDetailsObject:playerDetails];
或更简单,使用反比关系:
playerDetails.team = team;
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;
这篇关于iOS Core Data错误 - 一对多关系的不可接受的值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!