首页 > 解决方案 > 如果 Self 是两个(或更多)类中的一个,是否允许使用协议?

问题描述

我正在做一个我希望 UIView 和 UIViewController 都可以使用的协议

我试着做

    protocol ShowsResponse where Self: UIViewController, Self: UIView {
    ...
    }

但正如错误所示,它解释为 Self 应该是 UIViewController 和 UIView

是否有类似“协议 ShowsResponse where Self: UIViewController”或“Self: UIView”

标签: swiftprotocols

解决方案


Why don't you just try write down a protocol and write extension for UIViewController and UIView...

for example

 extension UIView: ShowsResponse {
 ...
 }

 extension UIViewController: ShowsResponse {
 ...
 }

 protocol ShowsResponse {
 ...
 }

I think this should solve your problem..


推荐阅读