ios – CoreData to-many添加错误

前端之家收集整理的这篇文章主要介绍了ios – CoreData to-many添加错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
不知道我在这里做错了什么.

学校对学生有很多,学生有反向.

一点测试代码如下:

  1. @class Student;
  2.  
  3. @interface School : NSManagedObject
  4.  
  5. @property (nonatomic,retain) NSString * name;
  6. @property (nonatomic,retain) NSOrderedSet *students;
  7. @end
  8.  
  9. @interface School (CoreDataGeneratedAccessors)
  10.  
  11. - (void)insertObject:(Student *)value inStudentsAtIndex:(NSUInteger)idx;
  12. - (void)removeObjectFromStudentsAtIndex:(NSUInteger)idx;
  13. - (void)insertStudents:(NSArray *)value atIndexes:(NSIndexSet *)indexes;
  14. - (void)removeStudentsAtIndexes:(NSIndexSet *)indexes;
  15. - (void)replaceObjectInStudentsAtIndex:(NSUInteger)idx withObject:(Student *)value;
  16. - (void)replaceStudentsAtIndexes:(NSIndexSet *)indexes withStudents:(NSArray *)values;
  17. - (void)addStudentsObject:(Student *)value;
  18. - (void)removeStudentsObject:(Student *)value;
  19. - (void)addStudents:(NSOrderedSet *)values;
  20. - (void)removeStudents:(NSOrderedSet *)values;
  21. @end
  22.  
  23.  
  24.  
  25. // Meanwhile,elsewhere...
  26. -(void)go {
  27. AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  28. NSManagedObjectContext *context = [app managedObjectContext];
  29.  
  30. School *school = (School *)[NSEntityDescription insertNewObjectForEntityForName:@"School" inManagedObjectContext:context];
  31. [school setName:@"Stanford"];
  32.  
  33. Student *student = (Student *)[NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];
  34. [student setName:@"Eric"];
  35.  
  36.  
  37. //[school addStudentsObject:student];
  38.  
  39. NSMutableSet *students = [school mutableSetValueForKey:@"students"];
  40. [students addObject:student];
  41.  
  42.  
  43. NSError *__autoreleasing error;
  44. BOOL success = [context save:&error];
  45.  
  46. if (!success) {
  47. @throw [NSException exceptionWithName:NSGenericException
  48. reason:[error description]
  49. userInfo:nil];
  50. }
  51. }

使用注释的addStudentsObject:失败:

  1. 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'
  2. *** First throw call stack:
  3. (0x1fa2012 0x13dfe7e 0x2030a4a 0xb85ec6 0xb087f9 0xb85d33 0x11aa638 0x7ee2069 0x2b4d 0xacd5b3 0x1f61376 0x1f60e06 0x1f48a82 0x1f47f44 0x1f47e1b 0x1efc7e3 0x1efc668 0x13ffc 0x1bdd 0x1b05)
  4. libc++abi.dylib: terminate called throwing an exception

使用mutableSetValueForKey:失败

  1. 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''
  2. *** First throw call stack:
  3. (0x1fa3012 0x13e0e7e 0x11370da 0x3af3 0xace5b3 0x1f62376 0x1f61e06 0x1f49a82 0x1f48f44 0x1f48e1b 0x1efd7e3 0x1efd668 0x14ffc 0x2b7d 0x2aa5)
  4. libc++abi.dylib: terminate called throwing an exception

解决方法

您可能想尝试将其添加到集合中:
  1. mutableOrderedSetValueForKey:

因为你选择了许多关系来订购.
但我相信你更容易以另一种方式设置关系:

  1. student.school = school;

猜你在找的iOS相关文章