Why can't I call the default super.init() on UIViewController in Swift?

SirRupertIII picture SirRupertIII · Jun 7, 2014 · Viewed 57k times · Source

I am not using a UIViewController from a storyboard and I want to have a custom init function where I pass in an NSManagedObjectID of some object. I just want to call super.init() like I have in Objective-C. Like this:

init(objectId: NSManagedObjectID) {
    super.init()
}

But I get the following compiler error:

Must call designated initializer of the superclass UIViewController

Can I simply not do this anymore?

Answer

occulus picture occulus · Jun 7, 2014

The designated initialiser for UIViewController is initWithNibName:bundle:. You should be calling that instead.

See http://www.bignerdranch.com/blog/real-iphone-crap-2-initwithnibnamebundle-is-the-designated-initializer-of-uiviewcontroller/

If you don't have a nib, pass in nil for the nibName (bundle is optional too). Then you could construct a custom view in loadView or by adding subviews to self.view in viewDidLoad, same as you used to.