Search code examples
objective-ccore-dataprivatetransient

Unable to make a Core Data transient attribute private


I have a Core Data entity Series with a transient attr indexCurrent. When outside classes access indexCurrent, I want them to send in an arg that can be used to check whether indexCurrent’s value needs to be updated before returning it. Therefore, I have declared indexCurrent as a private variable, and allow outside access to it only through that special method with an arg.

But although the compiler issues "method not found" warnings, it allows outside classes to call both indexCurrent and setindexCurrent:, and this faulty code executes with complete success.

Here is the Series interface:

@interface Series : NSManagedObject {
@private
NSNumber *indexCurrent;  
}

indexCurrent is not propertized, is not declared as dynamic in the implementation file, and I have not written indexCurrent or setindexCurrent: accessors.

What am I doing wrong? How can I make indexCurrent private?


Solution

  • @dynamic doesn't cause any code to be generated. Core Data generates the code for property accessors whether or not you use @dynamic. @dynamic just informs the compiler that code will be generated, so it doesn't need to warn about missing methods. That's why you get warnings but no runtime errors.

    The @private on the instance variable doesn't do much. The default is @protected, which means outside classes can't access it anyway, only the class itself and subclasses. In any case, the default Core Data accessors don't use instance variables.

    I'm not sure how to do what you want.