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

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

问题描述

NSManagedObject子类属性在其类别文件中创建,这是非常有线的,因为类别只能有方法。详细信息如下:



(1)。我在.xcdatamodeld文件中创建了一个名为BibleAudio的实体,其中包含以下几个属性。





(2)。 xcode生成的目标c文件是BibleAudio + CoreDataProperties.h,BibleAudio + CoreDataProperties.m和BibleAudio.h,BibleAudio.m如下:





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



BibleAudio + CoreDataProperties.h

有人知道我的理解是否正确?

解决方案

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



很大的缺点是
是可以在类类别中添加方法,而不是
实例变量。所以你不能添加一个简单的属性
(由一个实例变量备份)。一个可能的解决方法是
在实体中定义一个 transient属性,但这也有
的缺点。



em>现在 Xcode创建了一个类BibleAudio(在 BibleAudio.h / .m )基本上是空的,
和类别BibleAudio (CoreDataProperties)in
BibleAudio + CoreDataProperties.h / .m
类别文件包含所有Core Data属性,并且是



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


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).I have created an entity called BibleAudio in .xcdatamodeld file with several attributes as below.

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

(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?

解决方案

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.

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.

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.

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天全站免登陆