Skip to content

Instantly share code, notes, and snippets.

@bjhomer
Last active August 29, 2015 14:20
Show Gist options
  • Save bjhomer/2f3dab6fbbdb1727c0b7 to your computer and use it in GitHub Desktop.
Save bjhomer/2f3dab6fbbdb1727c0b7 to your computer and use it in GitHub Desktop.
Magical initializer delegation
class Top {
let x: Int
init() { x = 3; println("Top.init") }
}
class Bottom: Top {
var z: String
// This doesn't call `super.init`, but Top.init still runs. Why?
init(dummy: Bool) { z = "hi" }
}
let b = Bottom(dummy: true)
@bjhomer
Copy link
Author

bjhomer commented May 8, 2015

It seems that if a superclass has only one designated initializer, and it takes no parameters, then super.init is implicitly called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment