NS_REQUIRES_SUPER

__attribute((objc_requires_super)) was first introduced as work in progress into CLANG in September 2012 and was documented in October 2013. On both OS X and iOS there is now a NS_REQUIRES_SUPER macro that conditionally wraps the objc_requires_super attribute depending on compiler support. Once a method declaration is appended with this macro, the compiler will produce a warning if super is not called by a subclass overriding the method. E.g.:

@interface KPDObject : NSObject
- (void)reduceSize NS_REQUIRES_SUPER;
@end

@interface KPDDataObject : KPDObject
//...
@end

@implementation KPDDataObject
//...
- (void)reduceSize
{
    [self gzipDataIfNeeded];
}
@end

This will result in a warning: KPDDataObject: Method possibly missing a [super reduceSize] call.

Correctly marking methods with this macro is incredibly useful and should greatly reduce the hard to debug bugs introduced by the lack of calling super.

Intentionally not calling super

NS_REQUIRES_SUPER is great to catch mistakes, but there are always cases when circumstances call for breaking the rules. If you are sure that you don't want to call super you can always temporarily ignore the warning for that method, i.e.:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-missing-super-calls"
- (void)reduceSize
{
    [self gzipDataIfNeeded];
}
#pragma clang diagnostic pop
comments powered by Disqus