为什么我不能在 super 上调用核心数据访问器?

发布于 2024-09-30 19:31:42 字数 1619 浏览 1 评论 0原文

背景

我正在使用相当出色的 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

问题

  1. 为什么 super 不响应addChildrenObject: 方法?是否与以类别样式添加这些有关?

  2. 如何从子类访问 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

  1. Why does super not respond to the addChildrenObject: method? Is it something to do with these being added in a category style?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年少掌心 2024-10-07 19:31:42

如果您有两个 NSManagedObject 子类,其中父类实现动态属性,而其子类(NSManagedObject 的孙子)重写该属性的方法,则这些重写无法调用 super。

ADC

你不能调用super 因为这些访问器是由运行时按需生成的

If you have two subclasses of NSManagedObject where the parent class implements a dynamic property and its subclass (the grandchild of NSManagedObject) overrides the methods for the property, those overrides cannot call super.

says ADC

You can not call super as those accessors are generated on demand by the runtime

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文