Protected methods in Objective-C

LK. picture LK. · Sep 16, 2010 · Viewed 55.7k times · Source

What is the equivalent to protected methods in Objective-C? I want to define methods which only the derived classes may call/implement.

Answer

Brian Westphal picture Brian Westphal · May 23, 2011

You can simulate protected and private access to methods by doing the following:

  • Declare your private methods in a class extension (i.e. a unnamed category declared near the top of the class' .m file)
  • Declare your protected methods in a Subclass header – Apple uses this pattern with respect to UIGestureRecognizer (see documentation and reference to UIGestureRecognizerSubclass.h)

These protections are not, as Sachin noted, enforced at runtime (as they are in Java, for example).