Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Extension for Core Data Methods

Can someone walk me through how to 'hide' the standard core data setters?

I know there is not really a way to define 'Private' methods in Objective-C, but read about using an extension to achieve a similar result. The problem is, I want to apply this to core data classes. I would like to hide the standard setters created for some attributes, and only call them from inside other, exposed setters.

An example: My core data object has a BOOL 'collected' and a date 'dateCollected'. I have figured out how to add setDateCollected to setCollected, but now I would like to 'hide' set collected so that it can't be called directly so easily (when I might forget to also set dateCollected manually).

To clarify, the part that is tripping me up is the '@dynamic' calls - I don;t know where these should live.

EDIT - I guess I missed a part. I can move the @property declaration into the implementation file just fine. But I want the setter to be hidden, and the getter to remain public. I guess I need to replace the @property, but I don't know how to do this for a core data object.

like image 628
Ben Packard Avatar asked Mar 06 '26 06:03

Ben Packard


1 Answers

What you are trying to do is not likely to lead to a good result. Core Data classes are very delicately set up with graph hierarchies and the property implementations live in the superclass. Perhaps you should elininate the collected variable and just do a nil check on dateCollected. Another slightly more convoluted way would be to remove it from the data model and make it a regular ivar.

EDIT: Alright, I took a look at the info in your comment. It does indeed suggest that you can override the implementation if you adhere to certain guidelines. However, the answer below is probably better. Move the property to the private interface. Then declare another property in the public interface (readonly) that returns the value of the private property ;).

like image 193
borrrden Avatar answered Mar 07 '26 19:03

borrrden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!