Michael
Michael

Reputation: 33297

Call initializer from a Super Super Class in Swift?

I have a subclass of UITableViewController:

class ClassA : UITableViewController {

    // MARK: Init
    convenience init() {
        self.init(style: .Grouped)
    }

    convenience init(form: FormDescriptor) {
        self.init()
        self.form = form
    }

    override init(style: UITableViewStyle) {
        super.init(style: style)
    }

    override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        baseInit()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        baseInit()
    }    
}

I made a subclass of ClassA:

class ClassB : ClassA {

  override init(style: UITableViewStyle) {
    super.init(style: style)
  }

  convenience init() {
    self.init(style: .Grouped)
    self.form = getForm()
  }

  required init(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
}

I tried to init ClassB with the initializer from UITableViewController which uses the UITableViewStyle. When running the code I get:

fatal error: use of unimplemented initializer init(nibName:bundle:)

How can I init ClassB with the style property from UITableViewController?

Upvotes: 0

Views: 479

Answers (1)

Azat
Azat

Reputation: 6795

Put that code in your ClassB:

override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

Then you will be able to init ClassB as follows:

let b = ClassB(style: UITableViewStyle.Grouped)

Upvotes: 2

Related Questions