Simulating protected properties and selectors in Objective-C
There’s no protected modifier in Objective-C. In a sense, we should be grateful that Objective-C is not Java. On the other hand, sometimes it can suck. What’s the best way to deal with it? Let’s simulate protection!
The problem is already asked and answered
When looking for a solution to this, I found these two StackOverflow posts:
which exposed the real case where the lack of protected sucks (ie subclassing) and also the solution to the problem (ie simulating protection) as used by Apple herself in classes such as UIGestureRecognizer.
A Concrete Example
Let’s say I have a BSDaddy base class with only one publicly declared taskk:
Now I want a BSSon class, inheriting from BSDaddy.
… the protected…
Let’s say, Dad has information it should share with his son, like an address for instance. Let’s create a new file BSDaddySubclass.h in which we declared this kind of stuff.
Please not that I’m using () instead of something more precise like (subclass) cause otherwise, the properties are not auto-synthetized.
… and the private
Here is a basic implementation of Dad, in which we also defined stuff we want to keep private from Son.
Please note that we reuse () for the new interface declaration, it will not conflict with the one in BSDaddySubclass.h
Now let’s implement Son
Son is a rebel and want to share everything about his dad when he introduces himself.
BSDaddySubclass.h is imported.
The properties and tasks declared in BSDad.m can’t be used: you will get a compilation error if you try
The properties and tasks declared in BSDadSubclass.h CAN be used normally without the need of an extra @synthesize or @dynamic for properties.
Most importantly, please note that this is just simulated protection, which relies on a convention! If another class decides to import BSDadSubclass.h, nothing will prevent it to do so and use the protected methods as public ones. You just need to establish the convention that this is bad programming within your team. To make this even clearer, remember as well that there is no such things as private methods either. If you know a private method exists, you can still call it. That’s the power of Objective-C and remember what Uncle Ben said: “With great power comes great responsability”!
Optional: notice also the several #pragma that will prevent you to get warnings when trying to perform private selectors in your code. If you need them, you should be aware maybe you’re doing something nasty.
This is how George and Marty will introduce themselves:
You can find the full source code of this example on my iOS Demo code on GitHub.
And again, if you want to address me some feedback or comments, it’s via Twitter or GitHub.