Joris Kluivers

I like to build software.

Require a Call to Super in Overwritten Methods

Learned about a useful LLVM feature recently, which I tweeted about yesterday:

When trying this out for the first time in one of my projects it immediately warned me about a potential bug, where I forgot to call super.

Usage

Add the attribute annotation to the method declaration in your interfaces.

1
2
3
@interface MyObject : NSObject
- (void) myImportantMethod __attribute((objc_requires_super));
@end

When overwriting your important method in a subclass you will now be warned in case you forget to call super.

1
2
3
4
5
6
7
8
9
@interface SomeSubclass : MyObject
@end

@implementation SomeSubclass
- (void) myImportantMethod
{
  NSLog(@"Oops, might have forgotten something");
}
@end

Next time you compile you’ll be presented with the following:

This has been confirmed to be available as of Xcode 4.6.1