Swift协议扩展中允许混合类/协议类型约束吗?
问题描述:
是否可以在任何版本的Swift中扩展具有混合类/协议类型约束的协议?例如,仅当Self
是UIViewController
的子类并且符合Protocol1
时,我想扩展Protocol2
。Swift协议扩展中允许混合类/协议类型约束吗?
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 {}