我有一个自定义UITableViewCel(没什么花哨的)在Xcode 6.0上完美运行.当我尝试使用Xcode 6.1编译它时,编译器显示以下错误:
A non-failable initializer cannot chain to failable initializer 'init(style:reuseIdentifier:)' written with 'init?'
这是单元格的代码:
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() {<...>}
}
Run Code Online (Sandbox Code Playgroud)
作为解决方案,编译器提出Propagate the failure with 'init?':
override init?(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.setup()
}
Run Code Online (Sandbox Code Playgroud)
我有点困惑.是否可以详细说明什么是a (non)failable initialiser以及如何使用和覆盖?
Nat*_*ook 26
使用Swift 1.1(在Xcode 6.1中),Apple引入了可用的初始化程序 - 即可以返回nil而不是实例的初始化程序.你可以通过放置一个?后来定义一个可用的初始化程序init.您尝试覆盖的初始化程序在Xcode 6.0和6.1之间更改了其签名:
// Xcode 6.0
init(style: UITableViewCellStyle, reuseIdentifier: String?)
// Xcode 6.1
init?(style: UITableViewCellStyle, reuseIdentifier: String?)
Run Code Online (Sandbox Code Playgroud)
因此,要覆盖您需要对初始化程序进行相同的更改,并确保nil在创建单元格时处理大小写(通过指定可选项).
您可以在Apple的文档中阅读有关可用初始化程序的更多信息.
| 归档时间: |
|
| 查看次数: |
5960 次 |
| 最近记录: |