CoreData to-many add error

▼魔方 西西 提交于 2019-12-05 08:10:50

You might want to try this for adding to a set:

mutableOrderedSetValueForKey:

As you chose you to-many relationship to be ordered. but i believe that it will be easier for you to just set the relationship the other way:

student.school = school;

I had a similar problem. I read somewhere that the basic core data implementation for to-many relationships doesn't work correctly all the time. It has something to do with the relationship only accepting an nsset and the generated setters not properly converting the incoming arguments to an nsset. To fix it try overriding the addStudentsObject: method with something like this:

static NSString *const kItemsKey = @"students";

- (void)addStudentsObject:(Student *)value {
   NSMutableSet* tempSet = [NSMutableSet setWithSet:self.students];
   NSSet* newObject = [NSSet setWithObject:value];
   [self willChangeValueForKey:kItemsKey withSetMutation:NSKeyValueUnionSetMutation usingObjects:newObject];
   [tempSet addObject:value];
   self.students = tempSet;
   [self didChangeValueForKey:kItemsKey withSetMutation:NSKeyValueUnionSetMutation usingObjects:newObject];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!