class AView : UIView,ProtocolB{
}
class BView : UIView,ProtocolB{
}
let arr : UIView<ProtocolB>
我的两个子 view 都具备 ProtocolB,我想用一个 UIView 数组去装这两个子 view 的实例。 可以这样写吗?
1
finab 2021-04-20 14:53:51 +08:00
或者可以这样?
protocol ProtocolB: UIView { } class AView: UIView, ProtocolB { } class BView: UIView, ProtocolB { } let arr : [ProtocolB] arr.first?.backgroundColor = .white |
2
YYYeung 2021-04-25 01:15:30 +08:00
限制协议实现的类型
```swift protocol ProtocolB where Self: UIView {} class AView: UIView, ProtocolB {} class BView: UIView, ProtocolB {} let arr: [UIView] = [AView(), BView()] ``` |