Give warning when [super method] is not called

后端 未结 3 1970
一整个雨季
一整个雨季 2020-11-30 23:29

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 subc

相关标签:
3条回答
  • 2020-12-01 00:11
    NS_REQUIRES_SUPER
    

    is the modern expression for

    __attribute__((objc_requires_super))
    
    0 讨论(0)
  • 2020-12-01 00:21

    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.

    0 讨论(0)
  • 2020-12-01 00:25

    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
    
    0 讨论(0)
提交回复
热议问题