如何使所有Core Data对象从我的类而不是NSManagedObject继承? [英] How can I make all Core Data objects inherit from my class rather than NSManagedObject?

查看:79
本文介绍了如何使所有Core Data对象从我的类而不是NSManagedObject继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了自己的类,我希望使用Core Data而不是 NSManagedObject

I created my own class that I want Core Data to use instead of NSManagedObject:

@interface MyManagedObject: NSManagedObject {
  id delegate;
}

我不能使用类别,因为它声明了一个ivar。我的所有Core Data对象都使用具体类,而不是 NSManagedObject 的实例。我使用代码生成器来生成这些文件。下面是一个联系人对象的示例:

I can't use a category since this declares an ivar. All of my Core Data objects use concrete classes rather than being instances of NSManagedObject. I use the code generator to generate these files. Here's an example of what a Contact object might look like:

@interface Contact: NSManagedObject {
}



我知道我可以手动更改 NSManagedObject MyManagedObject ,但是我需要在每次重新生成代码时都这样做。有没有办法让它自动使用我自己的类?

I know that I can manually change NSManagedObject to MyManagedObject in this generated code, but I will need to do this every time I regenerate the code. Is there any way to make it automatically use my own class?

此外,任何解决方案中的 #import 是一个问题。理想情况下,我希望它使用 @class MyManagedObject 而不是 #importMyManagedObject.h,因为 MyManagedObject.h 位于库中,并且标题需要一个文件夹前缀可访问(例如 #importMyLib / MyManagedObject.h)。

Additionally, the #import in any solution might be a problem. Ideally I want it to use @class MyManagedObject rather than #import "MyManagedObject.h", since MyManagedObject.h is located in a library and the header needs a folder prefix to be accessible (e.g. #import "MyLib/MyManagedObject.h").

我尝试在 .xcdatamodel 文件具有相同的名称并指定所有对象从它继承,但有两个问题。第一个是它使用 #importMyManagedObject.h,它找不到由于我上面指定的原因。第二个问题是,我不确定这是一个好主意愚弄核心数据认为类继承自另一个Core Data对象...即使我不生成代码文件。我猜,Core Data可能在幕后做一些不必要的事情,因为它认为有一个额外的类,我的类继承自。

I tried creating a dummy object in the .xcdatamodel file with the same name and specifying that all objects inherit from it, but there are two problems. The first is that it is using #import "MyManagedObject.h", which it can't find due to the reason I specified above. The second problem is that I'm not sure it's a good idea to fool Core Data into thinking the class is inheriting from another Core Data object... even if I'm not generating the code file. I'm guessing that Core Data might be doing some unnecessary things behind the scenes because it believes there is an extra class which my classes are inheriting from.

我想我可以使用另一层类解决第一个问题。例如,我将指定对象继承自 MyProjectManagedObject 并在我的项目中创建一个 MyProjectManagedObject.h 文件: / p>

I guess I can solve the first problem using another layer of classes. For example, I would specify that the objects inherit from MyProjectManagedObject and create a MyProjectManagedObject.h file in my project:

#import "MyLib/MyManagedObject.h"

@interface MyProjectManagedObject: MyManagedObject {
}

...现在我的自动生成的文件将如下所示:

... and now my auto generated files will look like this:

#import "MyProjectManagedObject.h"

@interface Contact: MyProjectManagedObject {
}

...这将工作。唯一的问题是我上面提到的第二个关于Core Data中幕后运行的额外代码。这是担心还是应该忽略它?有没有其他方法来解决上述两个问题?

... which will work. The only problem is the second one I mentioned above about extra code running behind the scenes in Core Data. Is this something to worry about or should I ignore it? Are there any other ways to do this which solve both problems mentioned above?

推荐答案

看看MOGenerator。它将有助于至少重新生成托管对象类文件:它使每个文件有两个文件。一个您编辑的和一个自动生成的。当您重新生成后者时,前者是不变的。

Take a look at MOGenerator. It'll help with the regenerating the managed object class files at least: it makes you two files for each one. one that you edit and one that is automatically generated. When you regenerate the latter, the former is untouched.

http://digitalflapjack.com/blog/2010/mar/26/mogeneratorftw/

这篇关于如何使所有Core Data对象从我的类而不是NSManagedObject继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆