1

我们有一个奇怪的情况,行为从 Swift 4 -> 5 发生了变化,我不知道出了什么问题。我们在符合 UITableViewDatasource 和 UITableViewDelegate 的框架中有自己的开放基表视图控制器类。在 Swift 4 中,子类可以简单地实现这些委托方法中的任何一个,并且它们会按预期工作。在 Swift 5 和最新的 Xcode 中,这一切都发生了变化,那些协议中没有明确写在基类中的任何签名都将被完全忽略,从而导致各种奇怪的错误。

我们的基类实现看起来像这样:

open class OsuTableViewController: OsuViewController, UITableViewDelegate, UITableViewDataSource {
    public let tableView = UITableView(frame: .zero, style: .grouped)

    public var tableSections = [OsuTableSection]()


    override open func viewDidLoad() {
        super.viewDidLoad()
        tableView.datasource = self
        tableView.delegate = self
    }


    open func numberOfSections(in tableView: UITableView) -> Int {
        return tableSections.count
    }

    open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch tableSections[section].tableSectionType {
        default:
            return tableSections[section].rows.count
        }
    }

    open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(OsuTableViewCell.self)
        return cell
    }
}

在 Swift 4 中,任何子类都可以实现 didSelectRowAtIndexPath 或 viewForHeaderInSection,即使这些声明没有在基类中明确声明。

class AboutYouCustomizeTableViewController: OsuTableViewController {
    // With Xcode 10.2 and Swift 5 this is never called.
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
}

在 Swift 5 中,我们发现子类中的签名被简单地忽略了,除非我们开始写出基类中的每一个签名。这是痛苦的,有时会导致不良行为。这是有意更改还是错误?我们如何恢复旧的行为?

4

0 回答 0