类别中的 NSManagedObject 子类属性 [英] NSManagedObject subclass property in category

查看:20
本文介绍了类别中的 NSManagedObject 子类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSManagedObject 子类属性是在其类别文件中创建的,由于类别只能有方法,因此非常有线.详情如下:

The NSManagedObject subclass property are created in its category file, which is very wired since category can only have method. The details are as below:

(1).我在 .xcdatamodeld 文件中创建了一个名为 BibleAudio 的实体,具有如下几个属性.

(1).I have created an entity called BibleAudio in .xcdatamodeld file with several attributes as below.

(2).xcode 生成的目标 c 文件为BibleAudio + CoreDataProperties.h"、BibleAudio + CoreDataProperties.m"和BibleAudio.h"、BibleAudio.m",如下所示:

(2). the xcode generated objective c files are "BibleAudio + CoreDataProperties.h", "BibleAudio + CoreDataProperties.m" and "BibleAudio.h", "BibleAudio.m" as following:

(3).在BibleAudio + CoreDataProperties.h"中,BibleAudio 的属性在这里被声明为属性(见下文);而在BibleAudio.h"中,它是空的.据我所知,BibleAudio + CoreDataProperties.h"是一个类别文件,这里只能声明方法.因此我认为正确的方法是在BibleAudio.h"中声明属性,如果你想为这个 NSManagedObject 子类添加方法,你应该使用一个类别来添加该方法.

(3). within "BibleAudio + CoreDataProperties.h", BibleAudio's attributes are declared as property here (see following); while in "BibleAudio.h", it is empty. As far as I know, "BibleAudio + CoreDataProperties.h" is a category file and only method can be declared here. Thus the right way in my opinion is declare property in "BibleAudio.h", and if you want add method for this NSManagedObject subclass, you should use a category to add that method.

BibleAudio + CoreDataProperties.h

BibleAudio.h

有人知道我的理解是否正确吗?或者如果我错了,这背后的逻辑是什么?

Does anybody know if my understanding was right? or if I was wrong, what is the logical behind that?

推荐答案

在以前的 Xcode 版本中, 只为每个核心数据实体,例如BibleAudio.h/.m 中的BibleAudio"类.这些文件每次你都被覆盖重新创建了托管对象子类.因此,要添加您自己的 Core Data 类功能,您必须定义班级的类别(在单独的文件中).

In previous Xcode releases, only a class was created for each Core Data entity, e.g. the class "BibleAudio" in BibleAudio.h/.m. These files were overwritten each time you re-created the managed object subclasses. Therefore, to add your own functionality to the Core Data class, you had to define a category (in separate files) on the class.

最大的缺点是你可以在类类别中添加方法,但不能实例变量.所以你不能添加一个简单的属性(由实例变量支持).一种可能的解决方法是在实体中定义一个transient property,但这也有缺点.

The great disadvantage was that you can add methods in class categories, but not instance variables. So you could not add a simple property (backed up by an instance variable). One possible workaround was to define a transient property in the entity, but this had also disadvantages.

现在 Xcode 创建一个类BibleAudio"(在 BibleAudio.h/.m 中),它本质上是空的,和类别BibleAudio (CoreDataProperties)"BibleAudio + CoreDataProperties.h/.m类别文件包含所有核心数据属性,并且是重新创建托管对象子类时会被覆盖.

Now Xcode creates a class "BibleAudio" (in BibleAudio.h/.m) which is essentially empty, and a category "BibleAudio (CoreDataProperties)" in BibleAudio + CoreDataProperties.h/.m The category files contain all the Core Data properties, and are overwritten when you re-create the managed object subclasses.

类文件 BibleAudio.h/.m 只创建一次,永远不会被覆盖.您可以在那里添加功能:和以前一样的方法,还有自定义属性和实例变量.因为它是一个类而不是一个类别,旧的限制不再适用.

The class files BibleAudio.h/.m are created only once and never overwritten. You can add functionality there: methods as before, but also custom properties and instance variables. Because it is a class and not a category, the old restrictions don't apply anymore.

这篇关于类别中的 NSManagedObject 子类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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