I have a custom UITableViewCel (nothing fancy) that works perfectly on Xcode 6.0. When I try to compile it with Xcode 6.1 the compiler shows the following error:
A non-failable initializer cannot chain to failable initializer 'init(style:reuseIdentifier:)' written with 'init?'
Here is the code of the cell:
class MainTableViewCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.setup()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setup()
}
func setup() {<...>}
}
As a solution the compiler proposes Propagate the failure with 'init?'
:
override init?(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.setup()
}
I'm a bit confused.
Is it possible to elaborate what is a (non)failable initialiser
and how it should be used and overrided?
With Swift 1.1 (in Xcode 6.1) Apple introduced failable initializers -- that is, initializers that can return nil
instead of an instance. You define a failable initializer by putting a ?
after the init
. The initializer you're trying to override changed its signature between Xcode 6.0 and 6.1:
// Xcode 6.0
init(style: UITableViewCellStyle, reuseIdentifier: String?)
// Xcode 6.1
init?(style: UITableViewCellStyle, reuseIdentifier: String?)
So to override you'll need to make the same change to your initializer, and make sure to handle the nil
case (by assigning to an optional) when creating a cell that way.
You can read more about failable initializers in Apple's documentation.
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