A message to super
is sent to self
. However, the lookup of the implementation starts at a different place. When you message self
, the Objective-C run time (objc_msgSend()
, etc.) looks first in the class pointed to by self
's isa
pointer. If no implementation is found there, the search moves to the superclass, and so on.
When you message super
, the search starts in the superclass of the class whose code contained the invocation of super
. Yes, the message to super
translates into a call to objc_msgSendSuper()
or one of its variants. The compiler constructs an objc_super
structure that contains self
and the superclass of the class currently being compiled. (Note: that's not the same as [self superclass]
. That expression is dynamic and depends on the actual class of self
. The superclass targeted by a message to super
is not dynamic and doesn't depend on the actual class of self
.)
Anyway, objc_msgSendSuper()
uses the superclass referenced in the objc_super
structure to control where it starts the search for the implementation, but otherwise behaves like objc_msgSend()
. That's it. That's all that super
does: it starts the search for the implementation further along the chain of classes.
As to how to accomplish your swizzling goal… I think you want to first try adding an implementation that calls super
. If that fails (because the class already has such a method), then swizzle in a different implementation that calls the original.
So, something like:
@implemenation D (AdditionalWorkForHelloWorld)
- (void) addedHelloWorldIfNotPresent{
[super helloWorld];
NSLog(@"hello every one!");
}
- (void) additionalWorkForHelloWorld{
[self additionalWorkForHelloWorld];
NSLog(@"hello every one!");
}
@end
Then try adding the implementation of -addedHelloWorldIfNotPresent
using class_addMethod()
. Obtain the implementation using +[NSObject instanceMethodForSelector:]
with @selector(addedHelloWorldIfNotPresent)
. If adding the method fails, then swizzle in -additionalWorkForHelloWorld
for the existing -helloWorld
, instead.