Note that there are some explanatory texts on larger screens.

plurals
  1. POPrivate methods using Categories in Objective-C: calling super from a subclass
    text
    copied!<p>I was reading how to implement private methods in Objective-C (<a href="https://stackoverflow.com/questions/172598/best-way-to-define-private-methods-for-a-class-in-objective-c">Best way to define private methods for a class in Objective-C</a>) and a question popped up in my mind:</p> <p><strong>How do you manage to implement protected methods, i.e. private methods that are visible to subclasses?</strong></p> <p>Suppose I have a MySuperClass with a Category containing all its private methods, and I want to implement a MySubclass overriding or calling super to one of the MySuperClass private methods. Is that possible (using the Categories approach towards implementing private methods)? </p> <p>Take a look at some of this code, at the bottom there is the overriden method.</p> <pre><code>// =========================== // = File: MySuperClass.h // = Interface for MySuperClass // =========================== @interface MySuperClass : Object ... @end // =========================== // = File: MySuperClass.m // =========================== #import "MySuperClass.h" // ================================= // = Interface for Private methods // ================================= @interface MySuperClass (Private) -(void) privateInstanceMethod; @end // ===================================== // = Implementation of Private methods // ===================================== @implementation MySuperClass (Private) -(void) privateInstanceMethod { //Do something } @end // ================================ // = Implementation for MySuperClass // ================================ @implementation MySuperClass ... @end // =========================== // = File: MySubClass.h // = Interface for MySubClass // =========================== @interface MySubClass : MySuperClass ... @end // ================================ // = Implementation for MySubClass // ================================ #import MySubClass.h @implementation MySubClass //OVERRIDING a Super Private method. -(void) privateInstanceMethod { [super privateInstanceMethod]; //Compiler error, privateInstanceMethod not visible! //Do something else } @end </code></pre> <p>Hopefully somebody already figured this out.</p> <p>Cheers!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload