为什么我不能在 super 上调用核心数据访问器?
背景
我正在使用相当出色的 mogenerator 来自动生成我的核心数据访问器。
mogenerator 的类结构如下:
NSManagedObject
_JGTrainingBase
JGTrainingBase
_JGTrainingGroup
JGTrainingGroup
以下划线开头的类是由 mogenerator 使用核心数据访问器由机器生成的。
没有下划线的类是人类可编辑的类,因此您可以在其中放置自定义方法,并且在更改数据模型并重新运行 mogenerator 时不会覆盖它。
训练组实体具有称为“子级”的多对关系。
我正在使用核心数据访问器来修改我的关系。
我想要什么
我想在添加子对象之前更新持续时间 - 一个瞬态属性。
问题
我的代码
@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: 和类似方法将子对象添加到集合中,但这意味着我正在重写核心数据访问器,从而浪费了我的时间,并且可能使我的代码出现错误。我相信苹果的方法会比我写的任何方法都要好。
感谢您提供任何帮助我了解这里发生的事情的意见。
Background
I'm using the rather excellent mogenerator to auto generate my core data accessors.
mogenerator structures the classes as following:
NSManagedObject
_JGTrainingBase
JGTrainingBase
_JGTrainingGroup
JGTrainingGroup
Classes starting with an underscore are machine generated with core data accessors by mogenerator.
Classes without an underscore are human editable ones so you can put custom methods in there and not have it overwritten when you change your data model and rerun mogenerator.
The training group entity has to-many relationship called "children".
I'm using the Core Data accessors to modify my relationships.
What I Want
I want to update duration - a transient attribute - before adding a children object.
The Problem
My Code
@implementation JGTrainingGroup
...
-(void)addChildrenObject:(JGTrainingGroup *)value_ {
[self updateDuration];
[super addChildrenObject:value_];
}
...
@end
But when I call this method, I get an error message:
[JGTrainingGroup addChildrenObject:]: unrecognized selector sent to instance 0x10667fa30
Generated Code
@interface _JGTrainingBase : NSManagedObject {}
// Method declarations
@end
@interface _JGTrainingBase (CoreDataGeneratedAccessors)
- (void)addChildrenObject:(JGTrainingBase*)value_;
- (void)removeChildrenObject:(JGTrainingBase*)value_;
// Lots more methods
@end
Questions
Why does super not respond to the addChildrenObject: method? Is it something to do with these being added in a category style?
How can I access the Core Data generated method from a subclass?
Note
I realise I can add the children object to the set using primitiveValueForKey: and similar, but that means I'm rewriting the core data accessors, thus wasting my time and probably making my code buggy. I trust Apple's methods would be better than anything I could write.
Thanks for any input on helping me understand what's going on here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
说 ADC
你不能调用super 因为这些访问器是由运行时按需生成的
says ADC
You can not call super as those accessors are generated on demand by the runtime