如何以可在 Bindings 中访问的可重用方式修改 NSManagedObject 类的行为?

发布于 2024-11-05 03:09:52 字数 654 浏览 5 评论 0原文

Xcode 自动为您的 NSMO 实体生成类实现 - 太棒了。

但我们经常需要定制它们。如果您忘记自定义了这些文件,Xcode 会很乐意“删除”(覆盖)并删除您的所有代码。

所以...一个经典的技巧是:

  1. 在 Apple 的模型视图中创建 NSManagedObject
  2. 生成类 创建
  3. 使用类别来扩展原始类的新类,添加修改后的行为
  4. 导入自定义类别标头而不是基本 NSMO 标头,从而获得“新”行为

这非常有效:将自定义代码放入类别中,当您使用 Xcode 自动生成文件时,您永远不会丢失任何内容。

但是......现在我正在使用 Bindings / Mac OS 代码,并且 Bindings 很棒,但我不知道如何使 Binding “导入”派生标头(带有类别和修改后的方法/自定义行为)?

例如,如果我有一个保存 NSMO 实例的 ArrayController(非常常见),您通常会告诉它“实体名称”(例如“MyCoreDataEntity”),并且它会请求具有该类名称的 NSMO。但这永远不会加载类别,因此它永远不会获取该类的自定义版本。

你如何解决这个问题?或者:如何加载类的类别版本?

或者:如何在不使用类别的情况下编写自定义代码,并避免 Xcode 在需要时删除所有代码?

Xcode auto-generates the class implementations for your NSMO entities - great.

But we often need to customize them. If you ever forget that you customized these files, Xcode will happily "delete" (overwrite) and remove all your code.

So ... a classic trick was to:

  1. Create the NSManagedObject's in Apple's model view
  2. Generate the classes
  3. Create new classes which use Categories to extend the original classes, adding the modified behaviour
  4. Import the custom-category-headers rather than the base NSMO headers, thereby getting the "new" behaviour

This works great: put custom code in the category, and when you auto-generate files using Xcode, you never lose anything.

But ... now I'm using Bindings / Mac OS code, and Bindings are great, but I have no idea how to make a Binding "import" the derived header (with the category, and the modified methods / custom behaviour)?

e.g. if I have an ArrayController (very common) that's holding NSMO instances, you normally tell it the "Entity Name" (e.g. "MyCoreDataEntity"), and it requests the NSMO with that class name. But that will never load the category, so it will never pick up the customized version of the class.

How do you get around this? Either: how do you load in the category-version of a class?

OR: how do you write custom code without using categories and AVOID Xcode deleting all your code when it feels like it?

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

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

发布评论

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

评论(2

筑梦 2024-11-12 03:09:52

我必须承认,因为我使用可可触摸,所以没有可用的绑定 - 所以我真的不知道我的建议是否适用于您的情况。

然而,也许这有帮助?

我知道,有一种用于添加核心数据的类别的替代方案 - 不像类别那么“复杂”。

可以使用#include语句:

有两种选择:

  1. 创建一个新的ClassFile,删除包含“header.h”,(删除header.h),将额外的代码放在那里。它编译但带来两个警告:(这是可以理解的)[WARN]警告:没有规则来处理架构armv6的文本类型文件'$(PROJECT_DIR)/Classes/../included_dataStuff'[WARN]警告:没有规则来处理process file '$(PROJECT_DIR)/Classes/../included_dataStuff' of type text for Architecture armv7

  2. 创建一个新的“空”文件并将额外的代码放在那里。这不会产生任何警告。

1 和 2 之间的区别在于,虽然代码格式保留在第一个选项中(必须接受 2 个警告),但在第二个选项中,所有代码格式都丢失了,并且被视为普通文本(但没有警告),

我想我更喜欢第一个。当然,对生成的代码文件的唯一修改是#include 语句。

I must admit since I use cocoa touch there is no binding available - so I really do not know if my suggestion is applicable in your case.

However, maybe this helps?

There is an alternative to categories for core data additions - not as "sophisticated" as categories - I know.

One may use #include statements:

There are two alternatives:

  1. create a new ClassFile, delete the include "header.h", (delete the header.h), put the extra code there. It compiles but brings the two warnings: (which are understandable) [WARN]warning: no rule to process file '$(PROJECT_DIR)/Classes/../included_dataStuff' of type text for architecture armv6 [WARN]warning: no rule to process file '$(PROJECT_DIR)/Classes/../included_dataStuff' of type text for architecture armv7

  2. create a new "empty" file and put the extra code there. This does not produce any warnings.

The difference between 1 and 2 is that while the code formatting remains in the first alternatve (having to accept the 2 warnings) in the second all the code format is lost and its treated like normal text (but there is no warning)

I guess I would prefer the first. Of course, the only modification to the generated code file would be the #include statement.

还不是爱你 2024-11-12 03:09:52

最简单的解决方案是子类 NSArrayController,导入类别,然后在 IB 中使用该子类。这样您的绑定应该自动知道该类别。

MoGenerator 过去擅长生成自定义类,而无需覆盖任何内容。我正在修补 Xcode 4.x 的更新或类似内容,因为原始作者似乎没有时间这样做。

The easiest solution would be to subclass NSArrayController, import the category and then use the subclass in IB. That way your bindings should automatically know of the category.

MoGenerator used to be good at generating custom classes without having to overwrite anything. I'm tinkering with updating it or something like it for Xcode 4.x since the original authors don't seem to have the time to do so.

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