Valentin Shamardin Valentin Shamardin - 1 month ago 10
Objective-C Question

Add @propertys to Core Data class

I made few classes via Core Data. And I need some additional

@property
s for one of that classes in runtime. This
@property
s are responsible for download progress and I don't want to store them in Core Data DB. I tried to use a separate extension class:

@interface MyClass ()

{
CGFloat _downloadProgress;
NSInteger _downloadErrorCounter;
BOOL _downloadAllStuff;
BOOL _downloadUserCanceled;
}

@property (nonatomic, assign) CGFloat downloadProgress;
@property (nonatomic, assign) NSInteger downloadErrorCounter;
@property (nonatomic, assign) BOOL downloadAllStuff;
@property (nonatomic, assign) BOOL downloadUserCanceled;

@end


But private variables are not visible out of MyClass, and
@property
s compile all right, but in runtime i get
-[MyClass setDownloadErrorCounter:]: unrecognized selector sent to instance
.
Can anyone suggest me some solution?

Answer

The easiest solution (if you don't want to modify the Xcode generated class files) is to add the properties to the Core Data model and define the properties as transient. Transient properties are not saved to the store file.

Another option is to use a tool like "mogenerator", which generates two class files for each entity, one for the Core Data properties (which is overwritten if the the model changes), and one for your custom properties (which is not overwritten).

Update: Starting with Xcode 7, Xcode creates both a class and a category for each managed object subclass, compare NSManagedObject subclass property in category. Custom properties can be added to the class definition which is not overwritten when the model changes.

Comments