我有一个基类,它实现了一个符合协议的扩展,如下所示:
protocol OptionsDelegate { func handleSortAndFilter(opt: Options) } extension BaseViewController: OptionsDelegate { func handleSortAndFilter(opt: Options) { print("Base class implementation") } }
我有一个继承自BaseViewController的子类“InspirationsViewController”.我在扩展中覆盖协议方法如下:
extension InspirationsViewController { override func handleSortAndFilter(opt: Options) { print("Inside inspirations") } }
当我在子类扩展中覆盖“handleSortAndFilter”函数时,我收到错误:“扩展中的延迟无法覆盖”
但是当我实现UITableView数据源和委托方法时,我没有看到类似的问题.
如何避免这个错误?
解决方法
使用where子句的协议扩展.有用.
class BaseViewController: UIViewController { } extension OptionsDelegate where Self: BaseViewController { func handleSortAndFilter(opt: Options) { print("Base class implementation") } } extension BaseViewController: OptionsDelegate { } class InsipartionsViewController: BaseViewController { } extension OptionsDelegate where Self: InsipartionsViewController { func handleSortAndFilter(opt: Options) { print("Inspirations class implementation") } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。