Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2.0 Subclassing a subclass of UIViewController and called convenience initializers

Have a bit of confusion regarding designated and convenience initializers for UIViewController in Swift 2.0/Xcode 7beta3. Our UIViewControllers are all defined in code, there are no Nibs

Currently class A inherits from UIViewController like this

class A : UIViewController {
    convenience init() {
        ...
        self.init(nibName:nil, bundle:nil)
        ...
    }    
}

Then class B inherits from class A and should override the convenience init and call its as super.init()

class B : A {
    convenience init() {
        super.init()
        ...
    }    
}

The compiler does not allow this with Must call a designated initializer of the superclass '...' error on super.init()

like image 599
dmorrow Avatar asked Dec 02 '25 22:12

dmorrow


1 Answers

You need to make your initializers designated, not convenience:

class A : UIViewController {
    init() {
        super.init(nibName:nil, bundle:nil)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("")
    }
}

class B : A {
    override init() {
        super.init()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("")
    }
}

That gives you the inheritance structure you're looking for.

like image 120
matt Avatar answered Dec 04 '25 14:12

matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!