Kodeco Forums

Swift Tutorial: Initialization In Depth, Part 1/2

Take your Swift skills to the next level by learning about how your instances are initialized in this in-depth two-part tutorial on initialization!

This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1220-swift-tutorial-initialization-in-depth-part-1-2

Great tutorial René. The trick on using an extension to declare a custom init and keep the default member wise initializer is great!

Great tutorial. Since this just came out, might as well get these out of the way: change “occured” to “occurred”, “might into” to might run into", “throwing throwing” to “throwing” and add a link to part 2. Also, is there another tutorial that goes into throwing errors more? That is where my weakness lies. Again thanks for the great tutorial.

Good catches, thanks! I have fixed those issues. Thanks for the tutorial suggestion too :]

Hi René,
just for your information: the first image does not show Mars, but a total eclipse of the Moon :wink:

I think it’s worth mentioning that the default memberwise initializer has an access level of private if any property of the structure is private. It is internal else. So if you want a public initializer you have to implement one – so the automagic does not work if you want to use the structure from within another module/package even if the structure and all it’s properties are public. It’s not quite obvious to spot if you just see the source code and rely on automagic. [The Swift Programming Language (Swift 2.2): Access Control: Initializers]

Very informative tutorial. Sometimes Swift’s strict compiler makes you frustrating when it comes to initializing. Good to know what’s really going on now so we could easily figure them out.

Nice work on this tutorial! I had no idea we could do initialization extensions!

In Swift 2.2, class also can return nil anytime.:sunglasses:
You can try this.

Fortunately I’ve been able to understand tricky swifty inits :] Thanks for explanatory tutorial.

"As you can see, phase 1 begins with the call to the delegating initializer init(temperatureCelsius:pressureAtmospheric:) during which self cannot be used. "

I don’t understand this part…why can’t “self” be used…it works fine when I used self in the playground and it works.

I’m a bit late to this party, but that’s a great article René!

Small typo: Search for “initiaziler”

i am trying to get an error back from throwing init, but for some reason i am getting the warning on catch block. please see attached, am i missing something?

memberwise initializers only provide parameters for stored properties without default values.

Shouldn’t that be

Memberwise initializers only provide parameters for constants without default values and all variables.


Thanks for awesome tutorial! It clarify a lot for me. It would be great to learn something about required initializers, since we all use it with

required init?(coder aDecoder: NSCoder)

but I still don’t really understand, why all of my View Controllers need it.

This tutorial is more than six months old so questions regarding it are no longer supported for the moment. We will update it as soon as possible. Thank you! :]