加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – Xcode 6.1中的可用初始化器

发布时间:2020-12-15 02:00:38 所属栏目:百科 来源:网络整理
导读:我有一个定制的UITableViewCel(没有什么花哨),在Xcode 6.0上完美的工作。 当我尝试使用Xcode 6.1编译时,编译器显示以下错误: 不可用的初始化程序不能链接到可用的初始化程序’init(style:reuseIdentifier :)’用’init?’编写 这是单元格的代码: class
我有一个定制的UITableViewCel(没有什么花哨),在Xcode 6.0上完美的工作。
当我尝试使用Xcode 6.1编译时,编译器显示以下错误:

不可用的初始化程序不能链接到可用的初始化程序’init(style:reuseIdentifier :)’用’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() {<...>}
}

作为解决方案,编译器建议使用’init?’传播故障:

override init?(style: UITableViewCellStyle,reuseIdentifier: String?) {
    super.init(style: style,reuseIdentifier: reuseIdentifier)
    self.setup()
}

我有点困惑
是否可以阐述什么是(非)可用的初始化器,以及应如何使用和覆盖?

解决方法

使用Swift 1.1(在Xcode 6.1中),Apple引入了可用的初始化器 – 即初始化器可以返回零,而不是一个实例。您可以通过放置一个可用的初始化程序来定义?在init之后。您试图覆盖的初始化程序在Xcode 6.0和6.1之间更改其签名:

// Xcode 6.0
init(style: UITableViewCellStyle,reuseIdentifier: String?)

// Xcode 6.1
init?(style: UITableViewCellStyle,reuseIdentifier: String?)

因此,为了覆盖,您需要对初始化程序进行相同的更改,并确保以创建单元格的方式处理零个案例(通过分配给可选项)。

您可以阅读更多约failable initializers in Apple’s documentation。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读