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 necessary or not.
VideoView is subclass of UIView and it overrides two init methods, initWithFrame:
and initWithCoder:
. I know it's normal to override those init methods and then use some method for setting up other stuff, like - (void)setup;
.
But VideoView class uses static function, static void init(VideoView *self)
. Question is are there benefits when using static function vs. normal ObjC method?
Code in VideoView class looks like this:
-(id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
init(self);
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
init(self);
}
return self;
}
static void init(VideoView *self) { ... }
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With