Why it is throw an error as "Return from initializer without initializing all stored properties" in SwiftUI?

I have simple app in SwiftUI, when I use  @Binding var show : Bool for second register screen, it is throw an error for this line of code

  init(state: AppState) {
      
     self.viewModel = RegisterViewModel(authAPI: AuthService(), state: state)
      
   }

as a "Return from initializer without initializing all stored properties" any idea?

first screen:

import SwiftUI

struct RegisterFirstScreen: View {

  @Binding var show : Bool
  init(state: AppState) {
      
     self.viewModel = RegisterViewModel(authAPI: AuthService(), state: state) 
   }
  var body: some View {

   NavigationLink(destination: RegisterSecondScreen(state: viewModel.state, show: $show),
                           isActive: self.$pushActive) {
                Button {
          
                  viewModel.signUp()
                   
                } label: {
                  Text("Register Now")
                    .padding()
  
                }
              }
           }

second screen:

struct RegisterSecondScreen: View {

  @ObservedObject var state: AppState
   
  @Binding var show : Bool

  var body: some View {

    Text("Next main screen")

}}
Answered by skysoft13 in 706561022

when I use    @State var show = false in RegisterFirstScreen, it fix error.

Your init does not initialize your var "show".

Hence the message "Return from initializer without initializing all stored properties".

Accepted Answer

when I use    @State var show = false in RegisterFirstScreen, it fix error.

Why it is throw an error as "Return from initializer without initializing all stored properties" in SwiftUI?
 
 
Q