开发者

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜