Reputation: 701
So I want to do something like:
protocol CanShowView: class where Self: UIViewController, SomeDelegate{
func someFunction()
}
extension CanShowView{
func someFunction(){
someView.SomeDelegate = self
}
Basically, I want to make sure that the protocol can only be implemented by a UIViewController that also implements the SomeDelegate protocol, but xcode is mad about the first line of code in the example.
Upvotes: 2
Views: 682
Reputation: 2748
If you're using Swift 4+ :
protocol CanShowView {
func someFunction()
}
extension CanShowView where Self: UIViewController & SomeDelegate {
func someFunction() {
someView.SomeDelegate = self
}
}
If Swift 3.0:
protocol CanShowView {
func someFunction()
}
extension CanShowView where Self: UIViewController, Self: SomeDelegate {
func someFunction() {
someView.SomeDelegate = self
}
}
This is the right way to add conditional extensions, not in protocol definition. If you want to limit protocol to some constraint, best way to go is using associatedType
s.
Upvotes: 6