开发者

How to set a relation of an object of Core Data entity

I have an entity mainEntity with three one-to-many re开发者_JAVA百科lations to three different entities entity1, entity2 and entity3 (relations are named after objects they're referring to).

entity mainEntity
attribute name
relation entity1
relation entity2
relation entity3

These three entities have one-to-many reverse relations to this mainEntity (all the same, here's the example of first).

entity entity1
attribute name
relation mainEntity

So overall it's three many-to-many relations going from one mainEntity.

Now I need to set the three relations of mainEntity to object1, object2 and object3, which are instances of entity1, entity2 and entity3.

I can't do this:

MainEntity *myEntity = (MainEntity *)[NSEntityDescription
insertNewObjectForEntityForName:@"myEntity" inManagedObjectContext:context];
[myEntity setEntity1:object1];
[myEntity setEntity2:object2];
[myEntity setEntity3:object3];

Console says: Unacceptable type of value for to-many relationship: property = "entity1"; desired type = NSSet;

How to set them properly?


It sounds to me like you're trying to assign a single entity to a one-to-many relationship. If you only intended for entity1, entity2, and entity3 to each point to a single entity at a time, the easiest (and correct) way to fix this would be to change these relationships from one-to-many to one-to-one. If, however, you do want each of these to be a one-to-many relationship, I would replace

[myEntity setEntity1:object1];
[myEntity setEntity2:object2];
[myEntity setEntity3:object3];

with the following automatically-generated to-many relationship mutator methods

[myEntity addEntity1Object:object1];
[myEntity addEntity2Object:object2];
[myEntity addEntity3Object:object3];

For more information, check out Managed Object Accessor Methods in the Core Data Programming Guide.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜