3

I am trying to implement VIPER using SwiftUI. Firstly, I was returning a UIKit VC(UIHostingController(rootView)) from the Wireframe until I realized that in navigation, when presenter tells Wireframe to present a SwiftUI View, I can not pass a UIViewController trough a NavigationLink.

Having this issue, I tried to make Wireframe protocol to return a SwiftUI View, but it seems to be no possible

Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements

final class HomeWireFrame: HomeWireFrameProtocol {
//Error here returning a View
class func createHomeModule() -> View {

    var view = HomeView()
    //set up VIPER modules...
    .
    .

    return view

How can a I define a function that will return a SwiftUI View?

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Andoni Da Silva
  • 1,161
  • 15
  • 31

2 Answers2

2

Your Presenter class should have a variable of the protocol that the view implements. So lets say that your Presenter who implements the ModuleInput and ModuleOutput Protocols has 2 variables on for the view of type ViewInput Protocol and one for the router of type RouterInput Protocol.

So you router Input classes and Router should be like this

 protocol RouterInput {
    func routeToAnotherView(from view: ViewInput)
}

class Router: RouterInput {
    func routeToAnotherView(from view: ViewInput){
        guard let viewInMyType = view as? MyType else {
            return
        }
        // Now you can go to your view
    }
}

And from presenter you will call the function with the parameter your View protocol outlet.

Vasilis D.
  • 1,416
  • 13
  • 21
0

I have solved this issue returning a "HomeViewProtocol" instead a View... My HomeView conforms the HomeViewProtocol and of course, is a SwiftUI view.

Andoni Da Silva
  • 1,161
  • 15
  • 31