将自定义方法添加到子类 NSManagedObject

发布于 2024-08-19 13:29:34 字数 669 浏览 2 评论 0原文

我有一个核心数据模型,其中有一个实体 A,它是一个抽象。实体 B、C 和 D 继承自实体 A。实体 A 中定义了多个属性,供 B、C 和 D 使用。

我想在模型代码中利用此继承。除了属性之外,我想知道是否可以向实体 A 添加方法,这些方法在其子实体中实现。

例如:

  1. 我向实体 A 的接口添加一种方法,该方法返回一个值并采用一个参数
  2. 我将该方法的实现添加到 A、B、C、D
  3. 然后,我调用 executeFetchRequest: 来检索B 的所有实例
  4. 我在检索到的对象上调用该方法,该方法应该调用 B 的实现中包含的方法的实现

我已经尝试过这一点,但是在调用该方法时,我收到:

[NSManagedObject 方法名称:]: 无法识别的选择器发送到实例

我认为这是因为 executeFetchRequest: 返回的对象是某种类型的代理对象。

有什么方法可以使用子类 NSManagedObjects 来利用继承吗?

我真的希望能够做到这一点,否则我的模型代码将负责确定它正在处理的 NSManagedObject 类型并根据类型执行特殊逻辑,这是不可取的。

感谢任何帮助,提前致谢。

I have a Core Data model where I have an entity A, which is an abstract. Entities B, C, and D inherit from entity A. There are several properties defined in entity A which are used by B, C, and D.

I would like to leverage this inheritance in my model code. In addition to properties, I am wondering if I can add methods to entity A, which are implemented in it's sub-entities.

For example:

  1. I add a method to the interface for entity A which returns a value and takes one argument
  2. I add implementations of this method to A, B, C, D
  3. Then, I call executeFetchRequest: to retrieve all instances of B
  4. I call the method on the objects retrieved, which should call the implementation of the method contained in B's implementation

I have tried this, but when calling the method, I receive:

[NSManagedObject methodName:]:
unrecognized selector sent to instance

I presume this is because the objects returned by executeFetchRequest: are proxy objects of some sort.

Is there any way to leverage inheritance using subclassed NSManagedObjects?

I would really like to be able to do this, otherwise my model code would be responsible for determining what type of NSManagedObject it's dealing with and perform special logic according to the type, which is undesirable.

Any help is appreciated, thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

我不是你的备胎 2024-08-26 13:29:34

它应该有效。 executeFetchRequest: 返回的对象是 NSManagedObject(或其子类)的真实实例。
在CoreData中使用自定义类的步骤如下。假设您有实体 A 和 B,其中 B 继承自 A。
然后您需要两个自定义类,

@interface A:NSManagedObject{
}
-(void)someMethod:(NSString*)a;
@end;
@interface B:A{
}
-(void)someMethod:(NSString*)a;
@end;

然后将它们设置在 XCode 数据建模器中,如下所示:

screenshot of XCode modeler

这样,当从数据库中获取 NSManagedObject 时,CoreData 会自动将正确的类分配给它。

It should work. The objects returned by executeFetchRequest: are real instances of NSManagedObjects (or subclasses thereof.)
The steps to use custom classes in CoreData are as follows. Say you have entities A and B, where B inherits from A.
Then you need two custom classes as

@interface A:NSManagedObject{
}
-(void)someMethod:(NSString*)a;
@end;
@interface B:A{
}
-(void)someMethod:(NSString*)a;
@end;

Then set them in the XCode data modeler as shown:

screenshot of XCode modeler

This way, the CoreData automatically assigns the correct class to the NSManagedObject when it is fetched from the database.

画尸师 2024-08-26 13:29:34

如果您遇到该异常,则意味着 Core Data 未使用您的自定义类。这里的关键是 NSManagedObject ——这是 Core Data 为数据存储中的对象创建的对象。

如果您还没有这样做,您需要创建一个继承自 NSManagedObject 的类,在其中添加自定义方法,然后设置实体 A 以使用您的自定义类在对象模型工具中。如果实体 BCD 等具有特定行为,您应该为实体 A 创建的类进行子类化code> 并指定这些实体也使用子类。

本质上,您有一个并行的层次结构:一个实体层次结构,另一个类层次结构。对于对象模型中的每个实体,您最终可能会得到实体 X 和类 X

If you're getting that exception, it means Core Data is not using your custom class. The key here is NSManagedObject -- that's the object Core Data created for the objects in your data store.

If you haven't already, you'll need to create a class that inherits from NSManagedObject, add your custom methods there, and then set entity A to use your custom class in the object model tool. If entities B, C, D, etc. have specific behaviors, you should subclass the class you created for entity A and assign those entities to use the subclasses too.

Essentially, you have a parallel hierarchy: one hierarchy of entities, and another of classes. You'll likely end up with entity X and class X for each entity in your object model.

纸短情长 2024-08-26 13:29:34

在尝试使用我的自定义方法之前,在我的 NSManagedObject 子类上调用 isMemberOfClass 尝试了很多解决方案后,成功了。

[thing isMemberOfClass:[Thing class]];
[thing customMethod]; //was getting unrecognized selector sent to instance here before

After trying lots of solution calling isMemberOfClass on my NSManagedObject subclass before trying to use my custom method made the trick.

[thing isMemberOfClass:[Thing class]];
[thing customMethod]; //was getting unrecognized selector sent to instance here before
剪不断理还乱 2024-08-26 13:29:34

由于相同的根本原因,我也犯了同样的错误,但它是在不同的情况和不同的治疗方法下发生的。你的建议对我帮助很大!

最初,我创建了我的类来手动实现我的条目。我不知道有一个 Xcode 菜单。我认为该链接从未存在过!因此,直到我添加并开始测试新的自定义方法(不是 setter/getters),我才开始收到错误。

我的解决方案是更改类的名称,让 Xcode 通过 Editor->Create NS Mangage Object.... 为我的条目重新创建类,然后将旧代码剪切并粘贴到新班级。代码没有区别!

Xcode 似乎有某种在代码中不明显的内部链接。

I had this same error for the same underlying reason, but it came about in a different situation and a different cure. Your suggestion helped me a lot!

Originally I had created my class implementing my entry by hand. I didn't know there was an Xcode menu for this. I think the link was never there! So it wasn't until I had added and began testing the new custom methods (not setter/getters) that I started to get the error.

My solution was to change the name of my class, have Xcode re-create the class for my entry via Editor->Create NS Mangage Object.... Then cut and paste in the old code into the new class. No difference in code!

Xcode seems to have some kind of internal link that is not apparent in the code.

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