Swift: type 'any [protocol that conforms to view]' cannot conform to 'View'
I have a protocol that conforms to View
:
public protocol ProtocolView: View {
..开发者_如何学C.
}
And then I would like to have a View
that takes an array of views that conform to ProtocolView
and displays them:
public struct SomeView: View {
var model: Model
public var body: some View {
ScrollView {
ForEach(0..<model.views.count) { item in
model.views[item]
}
}
}
final class Model {
var views: [any ProtocolView]
init(views: [any ProtocolView]) {
self.views = views
}
}
}
However, when I do this, I get the following error:
type 'any ProtocolView' cannot conform to 'View'
Why does this happen, given that ProtocolView
does conform to View
, and how would I resolve this?
精彩评论