Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Cannot use 'super' as a reference?

I'm trying to use an NSInvocation to invoke a superclass method from the subclass. The code involved is relatively straightforward, it goes like:

- (NSInvocation*) invocationWithSelector:(SEL)selector {
    NSInvocation* call = [[NSInvocation alloc] init];
    [call retainArguments];
    call.target = super;  //ERROR:  use of undeclared identifier 'super'

    call.selector = @selector(selector);

    return call;
}

This seems a bit odd to me, as I had always assumed that super followed pretty much the same rules as self (i.e. it could be treated as a direct reference to the object in question and assigned to variables, used as a return value, etc.). It appears that this is not the case in practice.

Anyhow, is there any simple way to get my NSInvocation to target the superclass implementation (I cannot use self as the target, because the subclass overrides the superclass methods), or do I need to look for some other approach?

like image 964
aroth Avatar asked May 14 '12 06:05

aroth


2 Answers

See What exactly is super in Objective-C? for more info, but super is not actually an object. Super is a keyword for the compiler to generate obj-c runtime calls (specifically objc_msgSendSuper). Basically, it is like casting your class to its superclass before sending it the message.

EDIT So if you've overriden the method you want to call, you are going to have to write another method to call [super method] directly and set your invocation to call that method instead. The runtime will only send messages to objects, and they will be dealt with at the lowest member of the inheritance chain that implements them.

like image 165
borrrden Avatar answered Sep 18 '22 14:09

borrrden


super and self are the same object, so just use

call.target = self;

The difference between them is that you can use super as a receiver of a message to self as a way to ensure that it invokes the superclass' implementation.

like image 21
Monolo Avatar answered Sep 22 '22 14:09

Monolo