NSManagedObject subclass property in category

The NSManagedObject subclass property is created in the category file, which is very simple since the category can only have a method. Details are given below:

(1). I created an object called BibleAudio in a .xcdatamodeld file with several attributes, as shown below.

enter image description here

(2). xcode-generated object c files: "BibleAudio + CoreDataProperties.h", "BibleAudio + CoreDataProperties.m" and "BibleAudio.h", "BibleAudio.m", as shown below:

enter image description here

(3). in "BibleAudio + CoreDataProperties.h" the attributes of BibleAudio are declared here as a property (see below); but in "BibleAudio.h" it’s empty. As far as I know, "BibleAudio + CoreDataProperties.h" is a category file, and only a method can be declared here. So the correct way, in my opinion, is to declare a property in "BibleAudio.h", and if you want to add a method for this subclass of NSManagedObject, you should use a category to add this method.

BibleAudio + CoreDataProperties.h BibleAudio + CoreDataProperties.h

BibleAudio.h BibleAudio.h

Does anyone know if I understood correctly? or if I was wrong, which is logical behind?

0
source share
1 answer

In previous releases of Xcode, only the class Main data object, for example, was created for each class. class "BibleAudio" in BibleAudio.h/.m . These files were overwritten every time you recreated subclasses of managed objects. Therefore, to add your own functionality to the Core Data class, you had to define a category (in separate files) in the class.

The big drawback was that you can add methods to category categories, but not instance variables. Thus, you cannot add a simple property (backup copy of instance variable). One possible definition of a transient property is in essence, but these are also disadvantages.

Xcode now creates the BibleAudio class (in BibleAudio.h/.m ), which is essentially empty, and the BibleAudio (CoreDataProperties) category in BibleAudio + CoreDataProperties.h/.m The category files contain all the basic data properties and are overwritten when repeated subclassing a managed entity.

Class files BibleAudio.h/.m are created only once and are never overwritten. You can add functionality there: the methods are still, as well as custom properties and instance variables. Because it is a class and not a category, the old restrictions no longer apply.

+5
source

All Articles