NikMos NikMos - 4 months ago 9
iOS Question

Need to call super constructor for lifecycle methods in Swift

I've been coding for some years now, and I'm very used to always calling a method's super constructor when overriding it. But recently I've been wondering if it's always necessary... I get that when you override an initialiser you should call

super.init()
. But in Swift and iOS development, if you override a
UIViewController
lifecycle method like
viewDidLoad()
and forget to call
super.viewDidLoad()
, the compiler dosen't produce any warning or error and the code seems to be compiling fine. So I was wondering if the call is somehow build into the SDK, and if it's even necessary to make (with in mind that we want to make the code as efficient and correct as possible)?

Answer

I think you may look here a nice answer.

Apple doesn't restrict you to call super.viewDidLoad(), but it won't be safe to use some view cycle methods and variable before it's call. If you want first to init your own vars or properties for example - I think, it won't be a problem.

Comments