Benefits when using static init in Objective C?

前端 未结 1 1451
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 10:19

Recently I\'ve discovered webrtc-ios example from Github. While I was browsing though the project I noticed that VideoView class uses static method and I\'m not sure is that nec

相关标签:
1条回答
  • 2021-02-04 10:41

    One difference between using a static function and an Objective-C method is that the static function cannot be overriden in a subclass. If the common init code is done in a

    - (void)setup;
    

    method, and a subclass MyVideoView of VideoView happens to implement a method with the same name, then

    [[MyVideoView alloc] initWithFrame:..]
    

    will call the subclass implementation, which might not be wanted.

    In your code, initWithFrame/initWithCoder will always call the local init() function, even if an instance of a subclass is initialized.

    If the common initialization is done in a method, then the method name should be more specific, to avoid that it is overriden "accidentally", for example

    -(void)commonVideoViewSetup;
    
    0 讨论(0)
提交回复
热议问题