Note that there are some explanatory texts on larger screens.

plurals
  1. POwhat does the compiler when it finds [super msg];
    primarykey
    data
    text
    <p>I've read apple 'messaging' chapter from programming with objective - c and got several questions about self and super. AFAIK when compiler finds any message it translates it into objc_msgSend with two hidden parameters - receiver, selector and variable arguments for selector. for example <code>[self test]</code> will be something like that:</p> <pre><code>objc_msgSend(self, @selector(test)); </code></pre> <p>if there is no method implementation in receiver's dispatch table then function will try to find implementation in superclasses. super is just a flag for the compiler to start searching method implementation in the superclass of current object, and in documentation apple said that when compiler finds 'super' it translates it in something like that : </p> <pre><code>struct objc_super mySuperClass = { self, [self superclass] }; objc_msgSendSuper(&amp;mySuperClass, @selector(forwardedMethod)); </code></pre> <p>I've made a project with 3 classes, each one inherits from another. </p> <pre><code>@interface FirstClass : NSObject - (void)forwardMethod; @end @interface SecondClass : FirstClass @end @interface ThirdClass : SecondClass @end </code></pre> <p>I created an instance of third class in my root view controller and invoke his method called 'forwardMethod'. The implementation :</p> <pre><code>//First Class - (void)forwardMethod { NSLog(@"Base class reached"); } //SecondClass imp - (void)forwardMethod { NSLog(@"second class"); [super forwardMethod]; } //ThirdClass imp - (void)forwardMethod { NSLog(@"third class"); [super forwardMethod]; } </code></pre> <p>Everything works fine. But then i decided to interpret compiler:</p> <pre><code>//First Class - (void)forwardMethod { NSLog(@"Base class reached"); } //SecondClass imp - (void)forwardMethod { NSLog(@"second class"); struct objc_super mySuperClass = { self, [self superclass] }; objc_msgSendSuper(&amp;mySuperClass, @selector(forwardMethod)); } //ThirdClass imp - (void)forwardMethod { NSLog(@"third class"); struct objc_super mySuperClass = { self, [self superclass] }; objc_msgSendSuper(&amp;mySuperClass, @selector(forwardMethod)); } </code></pre> <p>Which results in an recursive call to the second class 'forwardMethod'. I create a struct in 'forwardMethod' at second class using self and [self superclass], but self is thirdclass and<br> my superclass will be always 'second class'. Maybe I'm doing something wrong, but how i can get to the base class 'forward method'?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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