|
Hey All, So I'm a bit confused looking at lessions two and three. In lesson two, we seem to hand-write an super initializer to an instance that is not called in the child class. The parent class contains it's own initializer:
but the child classes do not:
When we create an instance of Developer, Paul shows us calling the initializer to the parent class as part of the instance:
However, in lesson three, he discusses that we must call the parent initializers via super.init. Do we only have to call them if the child class also contains an initializer? If the child class does not contain an initializer and we call it, I presume this creates an issue of infilled values in the parent class since memberwise's arent available? Thanks, Andrew |
|
Hi!
Short answer is yes. Just as an explanation this is excerpt from book
As a side note, classes unlike structs do not have memberwise initializers. So we always have to use it with classes. You do not have to use init with classes only when you declare its properties and assign them on spot. In that case they have implicit init() like in example
but if you have it like:
|
|
|
|
|
|
here is another way of looking at it - if your subclass has a new property, it needs to be initialized as well - so you have to make a new init for the subclass:
and
so we need to reference super.init in the sub-class - hope this helps |
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.