6

不知道我在这里做错了什么。

School 对 Student 有一对多,Student 有它的逆。 在此处输入图像描述


在此处输入图像描述


一点测试代码如下:

@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
4

2 回答 2

8

您可能想尝试这个添加到集合中:

mutableOrderedSetValueForKey:

当您选择要订购的多对多关系时。但我相信以另一种方式设置关系会更容易:

student.school = school;
于 2013-04-14T05:08:56.893 回答
2

我有一个类似的问题。我在某处读到,对多关系的基本核心数据实现并不总是正常工作。它与只接受 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];
}
于 2013-04-14T01:32:34.877 回答