【问题标题】:Swift 3: Tableview datasource methods "Overriding non-open instance method outside of its defining module" errorSwift 3:Tableview 数据源方法“在其定义模块之外覆盖非开放实例方法”错误
【发布时间】:2017-08-18 19:13:44
【问题描述】:

我在核心框架中有一个 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()
  }
}

A类

import CustomCoreFramework

class ClassA : BaseViewController {

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

}

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

【问题讨论】:

  • 可能是因为方法被标记为 public 尝试删除 public 一次并检查,虽然之前没有尝试过,但不确定。
  • 你必须从 UIViewController 而不是 UITableViewDelegate 继承 BaseViewController——顾名思义——
  • 是否应该将 BaseViewController 的方法声明为打开?
  • @vadian 是的,我正在这样做,只是忘了把它放在帖子里。我已经更新了帖子。请检查。

标签: ios swift uitableview inheritance ios-frameworks


【解决方案1】:

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

What is the 'open' keyword in Swift?

【讨论】:

  • 我之前试过了,没用,现在可以了。我不知道为什么!!我很高兴它起作用了,也很生气,因为它以前不起作用。还是谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 2016-02-20
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
相关资源
最近更新 更多