Consider the following controller class with a delegate:
@objc protocol FooControllerDelegate {
}
@objc class FooController: UIViewController {
var delegate: FooControllerDelegate
init(delegate: FooControllerDelegate) {
self.delegate = delegate
super.init(nibName: nil, bundle: nil)
}
// TODO: How do we forbid this init?
required init(coder aDecoder: NSCoder) {
// TODO: Fails to compile.
super.init(coder: aDecoder)
}
}
Is there any way to forbid the usage of the -initWithCoder:
equivalent, without making the delegate implicitly unwrapped, and placing an assert(false)
inside the method?
Ideally, it won't be necessary to write the init(coder:)
with each subclass at all, and have it implicitly forbidden.
Overriding method must be accessible as it's enclosing type
init(coder:)
each time you adding custom initialiser then think about convenience
keyword. Swift's safety paradigm assumes that class either adds 'additional' init or has to modify behaviour of all required initialisers."Automatic Initializer Inheritance"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With