0

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")
              }
            }
          }
      }
    }
  }
}
1
  • Thanks @NiravD - if I remove route in {}, I get this error: Cannot find 'route' in scope. If you want to provide a full working code solution I would accept as an answer. Commented Sep 24, 2022 at 17:43

1 Answer 1

2

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")
                        }
                    }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.