背景
我正在使用非常出色的mogenerator来自动生成我的核心数据访问器。
mogenerator将类结构如下:
NSManagedObject
_JGTrainingBase
JGTrainingBase
_JGTrainingGroup
JGTrainingGroup
以下划线开头的类是由mogenerator生成的核心数据访问器机器生成的。
没有下划线的类是可编辑的人工类,因此您可以在其中放置自定义方法,并在更改数据模型并重新运行mogenerator时不被覆盖。
培训组实体有一个名为“children”的一对多关系。
我正在使用Core Data访问器来修改我的关系。
我的需求
在添加子对象之前,我想更新持续时间(transient attribute)。
问题
我的代码
@implementation JGTrainingGroup
...
-(void)addChildrenObject:(JGTrainingGroup *)value_ {
[self updateDuration];
[super addChildrenObject:value_];
}
...
@end
但是当我调用这个方法时,我收到一个错误信息:
[JGTrainingGroup addChildrenObject:]: unrecognized selector sent to instance 0x10667fa30
生成的代码
@interface _JGTrainingBase : NSManagedObject {}
// Method declarations
@end
@interface _JGTrainingBase (CoreDataGeneratedAccessors)
- (void)addChildrenObject:(JGTrainingBase*)value_;
- (void)removeChildrenObject:(JGTrainingBase*)value_;
// Lots more methods
@end
问题
为什么super不响应addChildrenObject:方法?这与这些方法是以类别的方式添加有关吗?
如何从子类中访问Core Data生成的方法?
注意
我知道我可以使用primitiveValueForKey:等方法将子对象添加到集合中,但这意味着我正在重写Core Data访问器,浪费时间并可能使我的代码出错。我相信苹果的方法比我能写的任何东西都要好。
感谢您对帮助我理解这里发生了什么的任何输入。