我有一个看起来像这样的协议(protocol) ShareDelegate
:
protocol ShareDelegate : class {
func share(event: EventJSONModel, skipToTime time: CMTime?, view: UIView, completion: @escaping () -> ())
}
extension ShareDelegate where Self: UIViewController {
func share(event: EventJSONModel, skipToTime time: CMTime? = nil, view: UIView, completion: @escaping () -> ()) {
}
}
然后当我将其用作委托(delegate)时:
weak var delegate: ShareDelegate?
然后调用委托(delegate)函数:
delegate?.share(event: event, view: view, completion: completion)
它给我以下错误
“ShareDelegate”要求“ShareDelegate”继承自“UIViewController”
如果我删除扩展的 skipToTime time: CMTime?
部分,它就可以正常工作。为什么??
最佳答案
问题是协议(protocol)和默认实现之间的接口(interface)不同。
协议(protocol):
func share(event: EventJSONModel, skipToTime time: CMTime?, view: UIView, completion: @escaping () -> ())
扩展名:
func share(event: EventJSONModel, skipToTime time: CMTime? = nil, view: UIView, completion: @escaping () -> ())
所以你已经在扩展中声明了 skipToTime
是可选的,并且有一个默认值,所以当调用它并跳过那个值时,你专门调用了 UIViewController 的版本
更新:
您应该能够限制 ShareDelegate 协议(protocol)的使用,以便它仅适用于 UIViewControllers,如下所示:
protocol ShareDelegate: class where Self: UIViewController {
func share()
}
关于ios - 协议(protocol)要求委托(delegate)继承自 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52402656/