Give warning when [super method] is not called

When not using ARC, you get a warning when not calling [super dealloc] in your dealloc method.

I'm trying to implement something similar with a class that gets subclassed often to warn the person implementing the subclass when they don't call super.

Any ideas?


Solution 1:

Recent versions of llvm have added an attribute that indicates that subclasses must call super:

@interface Barn:NSObject
- (void)openDoor NS_REQUIRES_SUPER;
@end

@implementation Barn
- (void) openDoor
{
    ;
}
@end

@interface HorseBarn:Barn
@end
@implementation HorseBarn
- (void) openDoor
{
    ;
}
@end

Compiling the above produces the warning:

Method possibly missing a [super openDoor] call

Solution 2:

UPDATE

See bbum's answer for a direct way to tell the compiler that subclassers must call super.

ORIGINAL

What Apple does generally is to provide a hook, like viewDidLoad. Apple doesn't do any work in the base -[UIViewController viewDidLoad]. It's an empty method. Instead, Apple does its work elsewhere, in some private method that you're not allowed to override, and calls your viewDidLoad from that method. So even if you forget to call [super viewDidLoad] in your UIViewController subclass, all of Apple's work still gets done.

Perhaps you can revise your API to use a similar pattern.

Solution 3:

NS_REQUIRES_SUPER

is the modern expression for

__attribute__((objc_requires_super))