1

I really like the Swift type-systems. But I cannot fathom why I cannot do this:

        var view: View
        if #available(tvOS 15.0, *) {
            view = ManageProfilesView.MainView(
                    viewModel: .init(),
                    action: { _ in }
            )
        } else {
            view = ManageProfilesView.LegacyView(
                    viewModel: .init(),
                    action: { _ in }
                )
        }
        let viewController = UIHostingController(rootView: view) // Type 'any View' cannot conform to 'View'
        presenter.show(viewController, sender: presenter)

It's clear that both views are correct conformances of the View protocol, so the let view: View should satifty the requirement of the UIHostingController<Content> : UIViewController where Content : View

Seto
  • 1,234
  • 1
  • 17
  • 33

1 Answers1

1

View is protocol that have associated type. It's mean that there is exist infinite number of View's protocols every with own associated type. You can imagine that at the true condition you assign int to view variable and at the false condition you trying to assign string variable.

You just need to create 3rd view and put that condition in it's body.

Cy-4AH
  • 4,370
  • 2
  • 15
  • 22