Jon Sakas
Jon Sakas

Reputation: 2242

NavigationStack Type '() -> ()' cannot conform to 'View'

I am building a simple Sign Up and Login user flow with SwiftUI in Xcode 14. I am attempting to use the NavigationStack struct, however I am receiving the error below.

I am seeking to correct this code, or to implement another non-deprecated way of using navigation in SwiftUI to control different application views. I would hope to expand on this further by creating additional views (forgot password, login with email link, etc).

import SwiftUI

enum Route : Hashable {
  case login
  case signup
  case app
}

@main
struct MyApp: App {
  var body: some Scene {
    WindowGroup {
      NavigationStack {
        ContentView()                                       // Type '() -> ()' cannot conform to 'View'
          .navigationDestination(for: Route.self) {         // Type '() -> ()' cannot conform to 'View'
            route in {
              switch route {
              case .login:
                Text("Login")
              case .signup:
                Text("Sign Up")
              }
            }
          }
      }
    }
  }
}

Upvotes: 0

Views: 392

Answers (1)

Nirav D
Nirav D

Reputation: 72460

You have added extra { after route in and because of this Xcode added } as well. So remove this extra start { and end }. Now after fixing this in your switch case you missed handling of app case because of this you will get Switch must be exhaustive error. To fix this add app case or default case as well in your switch. After fixing both issues your code will look like this.

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            NavigationStack {
                ContentView()
                    .navigationDestination(for: Route.self) { route in
                        switch route {
                        case .login:
                            Text("Login")
                        case .signup:
                            Text("Sign Up")
                        case .app:
                            Text("App")
                        }
                    }
            }
        }
    }
}

Upvotes: 2

Related Questions