0
votes

I have a enum

enum MenuLateralViewModel: Int, CaseIterable {
    case denuncia
    case autoConstatacao
    case notificacao
    case autoInfracao
    case debitos
    case profissional
    case sincronizacao
}

for each item i want to return a different view, i tried doing this:

   var destino: View {
        switch self {
        
        case .denuncia: return DenunciaFiltroView()
        case .autoConstatacao: return View2()
        case .notificacao: return View3()
        case .autoInfracao: return View3()
        case .debitos: return View5()
        case .profissional: return View6()
        case .sincronizacao: return View7()
        }
    }

but it gives me the error

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

How can i achieve this?

2

2 Answers

3
votes

Use AnyView instead of View

enum MenuLateralViewModel: Int, CaseIterable {
    case view1
    case view2
    
    var destino: AnyView {
         switch self {
         case .view1: return AnyView(ContentView())
         case .view2: return AnyView(ContentViewSecond())
         }
     }
}
1
votes

Use ViewBuilder, and an opaque return type.

@ViewBuilder var destino: some View {
  switch self {
  case .denuncia: DenunciaFiltroView()
  case .autoConstatacao: View2()
  case .notificacao: View3()
  case .autoInfracao: View3()
  case .debitos: View5()
  case .profissional: View6()
  case .sincronizacao: View7()
  }
}