Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 626
jordanperry Avatar asked Apr 18 '13 22:04

jordanperry


2 Answers

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
like image 128
bbum Avatar answered Oct 21 '22 13:10

bbum


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.

like image 6
rob mayoff Avatar answered Oct 21 '22 12:10

rob mayoff