bearMountain bearMountain - 1 year ago 130
Swift Question

Does a swift subclass *always* have to call super.init()

If I have a swift subclass that:

  1. doesn't need to access

  2. doesn't need to access any properties on

Do I still have to call
in the subclass's

*Note: This is a different question than the one asked and answered here on SO because of the specifics listed in 1 and 2 above.

Answer Source

No, you don't have to.

Assume you have the following classes.

class a {
    let name: String

    init() {
        name = "james"

class b: a {
    let title: String

    override init() {
        title = "supervisor"

If you instantiate a variable with

let myVar = b()


  • override init() in b will be called
  • then the init() in a will be called

Even though you didn't explicitly call super.init().

This has been confirmed by Chris Laettner on the swift-user's email list. It kicks in when your super class has a single designated initializer with a zero-argument init. This is why you don’t have to call super.init() when deriving from NSObject.

*Thanks to Wingzero's comment below

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download