Swift 3:Tableview 数据源方法“覆盖其定义模块之外的非开放实例方法”错误

Sru*_*mha 6 inheritance uitableview ios ios-frameworks swift

open BaseViewController在核心框架中有一个类,它实现了 tableview 数据源方法。假设我有另一个类(在模块之外)ClassABaseViewController因为它是超类。当我尝试覆盖 tableview 数据源方法时,它抛出了这个错误Overriding non-open instance method outside of its defining module

BaseViewController 看起来像这样

open class BaseViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

...

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 0
}

public func numberOfSections(in tableView: UITableView) -> Int {
    return 0
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    return UITableViewCell()
  }
}
Run Code Online (Sandbox Code Playgroud)

A类

import CustomCoreFramework

class ClassA : BaseViewController {

// throws an error
public override func numberOfSections(in tableView: UITableView) -> Int {
    return tableViewListItems.count
}

}
Run Code Online (Sandbox Code Playgroud)

我想open类方法应该可以在模块外部访问。我尝试将 tableview 方法访问说明符更改为public不同的组合,但似乎没有任何效果。

Xvo*_*lks 10

BaseViewController 的方法应该被声明为打开的。这是在参考线程中讨论的。

请参阅什么是 Swift 中的“open”关键字?