Ios CoreData向多个添加错误

Ios CoreData向多个添加错误,ios,core-data,one-to-many,Ios,Core Data,One To Many,不知道我做错了什么 学校对学生有多方面的影响,而学生则相反。 下面是一个小测试代码: @class Student; @interface School : NSManagedObject @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) NSOrderedSet *students; @end @interface School (CoreDataGeneratedAccess

不知道我做错了什么

学校对学生有多方面的影响,而学生则相反。



下面是一个小测试代码:

@class Student;

@interface School : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSOrderedSet *students;
@end

@interface School (CoreDataGeneratedAccessors)

- (void)insertObject:(Student *)value inStudentsAtIndex:(NSUInteger)idx;
- (void)removeObjectFromStudentsAtIndex:(NSUInteger)idx;
- (void)insertStudents:(NSArray *)value atIndexes:(NSIndexSet *)indexes;
- (void)removeStudentsAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectInStudentsAtIndex:(NSUInteger)idx withObject:(Student *)value;
- (void)replaceStudentsAtIndexes:(NSIndexSet *)indexes withStudents:(NSArray *)values;
- (void)addStudentsObject:(Student *)value;
- (void)removeStudentsObject:(Student *)value;
- (void)addStudents:(NSOrderedSet *)values;
- (void)removeStudents:(NSOrderedSet *)values;
@end



// Meanwhile, elsewhere...
-(void)go {
   AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
   NSManagedObjectContext *context = [app managedObjectContext];

   School *school = (School *)[NSEntityDescription insertNewObjectForEntityForName:@"School" inManagedObjectContext:context];
   [school setName:@"Stanford"];

   Student *student = (Student *)[NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];
   [student setName:@"Eric"];


   //[school addStudentsObject:student];

   NSMutableSet *students = [school mutableSetValueForKey:@"students"];
   [students addObject:student];


   NSError *__autoreleasing error;
   BOOL success = [context save:&error];

   if (!success) {
      @throw [NSException exceptionWithName:NSGenericException
                                     reason:[error description]
                                   userInfo:nil];
   }
}

使用已注释的
addStudentsObject:
失败,原因是:

2013-04-13 16:22:58.648 CDTMTest[2098:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSSet intersectsSet:]: set argument is not an NSSet'
*** First throw call stack:
(0x1fa2012 0x13dfe7e 0x2030a4a 0xb85ec6 0xb087f9 0xb85d33 0x11aa638 0x7ee2069 0x2b4d 0xacd5b3 0x1f61376 0x1f60e06 0x1f48a82 0x1f47f44 0x1f47e1b 0x1efc7e3 0x1efc668 0x13ffc 0x1bdd 0x1b05)
libc++abi.dylib: terminate called throwing an exception

使用
mutableSetValueForKey:
失败

2013-04-13 16:07:05.111 CDTMTest[2012:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSManagedObjects of entity 'School' do not support -mutableSetValueForKey: for the property 'students''
*** First throw call stack:
(0x1fa3012 0x13e0e7e 0x11370da 0x3af3 0xace5b3 0x1f62376 0x1f61e06 0x1f49a82 0x1f48f44 0x1f48e1b 0x1efd7e3 0x1efd668 0x14ffc 0x2b7d 0x2aa5)
libc++abi.dylib: terminate called throwing an exception

我也有类似的问题。我在某个地方读到,对多个关系的基本核心数据实现并不是一直都能正常工作。这与仅接受nsset的关系以及生成的setter没有正确地将传入参数转换为nsset有关。要修复此问题,请尝试使用以下内容覆盖addStudentsObject:方法:

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];
}

您可能希望尝试将以下内容添加到集合:

mutableOrderedSetValueForKey:
因为你选择了你要订购的许多关系。 但我相信,你可以更容易地用另一种方式建立关系:

student.school = school;

这可能只是一个输入错误,但我很困惑:您提到:“注释的
setStudentsObject
”导致了失败。但在您的问题文本中,注释行是
addStudentsObject
。这只是你问题中的一个输入错误,还是造成错误的原因?@Anthony:这是一个输入错误<代码>添加~正确,已修复。谢谢可能是@MartinR的副本谢谢你追踪到我认为可能已经在这里的东西。投票结束。我有空的时候会试试的。如果你能找出你读到的地方,请发一个链接。谢谢你的代码!你真的需要有序的关系吗?“无序的关系不会出现问题。@Martin是的,我希望保持有序。@我的特殊目的我认为这个答案会起作用,但因为我实际上是按照丹·雪莱的建议做的,所以我会接受他的。@psoft我知道它会起作用。我从我的一个live应用程序的代码库中取出了它;)但我不知道它在CoreData文档中。这是一段时间前,当我找到解决办法!这就是我目前正在做的,它确实正确地维护了订单。但是我仍然不知道为什么add方法不起作用,或者为什么它还没有在这里出现。它让我觉得我错过了什么。它已经出现了:。