Reputation: 8376
I have a protocol ShareDelegate
that looks like this:
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 () -> ()) {
}
}
Then when I use this as a delegate:
weak var delegate: ShareDelegate?
and then call the delegate function:
delegate?.share(event: event, view: view, completion: completion)
it gives me the following error
'ShareDelegate' requires that 'ShareDelegate' inherit from 'UIViewController'
If I remove the skipToTime time: CMTime?
part of the extension it works fine. Why??
Upvotes: 4
Views: 1837
Reputation: 19750
The issue is that the interface is different between the protocol and the default implementation.
protocol:
func share(event: EventJSONModel, skipToTime time: CMTime?, view: UIView, completion: @escaping () -> ())
Extension:
func share(event: EventJSONModel, skipToTime time: CMTime? = nil, view: UIView, completion: @escaping () -> ())
So you've declared skipToTime
to be optional in the extension with a default value, so when calling it and skipping that value, you are specifically calling the version that is confined to UIViewController
UPDATE:
You should be able to restrict usage of your ShareDelegate protocol so that it only works with UIViewControllers like this:
protocol ShareDelegate: class where Self: UIViewController {
func share()
}
Upvotes: 1
Reputation: 5088
extension ShareDelegate where Self: UIViewController {
func share(...) {
}
}
Because of this code above, you need a UIViewController
to confirm to the delegate using extension it would look something like that
extension UIViewController: ShareDelegate {
func share(...) {
}
}
Upvotes: 1