Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

ios - NSManagedObject subclass property in category

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.

enter image description here

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

enter image description here

(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 + CoreDataProperties.h

BibleAudio.h BibleAudio.h

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...