Swift 协议扩展中是否允许混合 class/protocol 类型约束?
Are mixed class/protocol type constraints allowed in Swift protocol extensions?
是否可以在 Swift 的任何版本中扩展具有混合 class/protocol 类型约束的协议?例如,仅当 Self
是 UIViewController
的子类并且符合 Protocol1
.
时,我想扩展 Protocol2
protocol Protocol1 {}
protocol Protocol2 {}
//What I imagine I could do, but it does not compile
extension Protocol2 where Self: UIViewController, Protocol2 {}
尝试:
extension Protocol2 where Self: UIViewController, Self: Protocol2 {}
是否可以在 Swift 的任何版本中扩展具有混合 class/protocol 类型约束的协议?例如,仅当 Self
是 UIViewController
的子类并且符合 Protocol1
.
Protocol2
protocol Protocol1 {}
protocol Protocol2 {}
//What I imagine I could do, but it does not compile
extension Protocol2 where Self: UIViewController, Protocol2 {}
尝试:
extension Protocol2 where Self: UIViewController, Self: Protocol2 {}