Let's assume for a moment that the compiler inlines a method:
@implementation AwesomeClass
- (void)doFoo OBJC_INLINE { // or some way to indicate "this is an inline method"
NSLog(@"doing foo!");
}
- (void)doBar {
[self doAwesomeStuff];
[self doFoo];
}
@end
so that -doBar essentially becomes:
- (void)doBar {
[self doAwesomeStuff];
{
NSLog(@"doing foo!");
}
}
Awesome, that seems like it'd be faster, right? We save ourselves a whole dozen instructions by not calling objc_msgSend. So you package this up and post it online as a .a file.
NSCleverCoder comes along and says "but I want doFoo to do a little bit more", so he does:
@interface SuperAwesomeClass : AwesomeClass @end
@implementation SuperAwesomeClass
- (void)doFoo {
NSLog(@"doing more foo!");
[super doFoo];
}
@end
When he tries to run this, it never gets called, because AwesomeClass never actually invokes the -doFoo method.
"But," you say, "this is a contrived example!"
No, it's not. In Objective-C, it is perfectly legal to do this at any point in the development or execution of an app. I can do this when writing the code. Heck, I can even do this at runtime by using objc_allocateClassPair and class_addMethod to dynamically create a subclass and add a method override.
I can also swizzle method implementations. Don't like the existing implementation of -doFoo? That's cool; replace it with your own. Oh wait; if the method was inlined, your new implementation would never get called, because -doBar is never actually invoking the -doFoo method.
The only time I could see this being possible is if there were some sort of way to annotate a method as being un-overridable.
answered 2011-11-30T18:11:54.693