jordanperry jordanperry - 2 months ago 20
Objective-C Question

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?

Answer

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

@interface Barn:NSObject
- (void)openDoor __attribute__((objc_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
Comments