eugene_prg
eugene_prg

Reputation: 1168

Initializing 2 variables in init(): 'self' used before all stored properties are initialized

I'm trying to initialize 2 variables

self.customerVM = BuildCustomerViewModel()
self.showSurvey = showSurvey.wrappedValue

inside of my init() function and xCode returns

'self' used before all stored properties are initialized

When i try to initialize just the 1st one and do not use the 2nd variable - everything goes smoothly.. I don't understand why..

I wonder how should i change the code to make it work. Any help is appreciated.

import SwiftUI

struct BuildCustomerView: View {
    @EnvironmentObject var thisSession: CurrentSession
    @ObservedObject var customerVM: BuildCustomerViewModel

    @State var fitnessLevel: Double = 0.0

    @Binding var showSurvey: Bool
    
    init(showSurvey: Binding<Bool>) {
        self.customerVM = BuildCustomerViewModel()
        self.showSurvey = showSurvey.wrappedValue
    }
    
    var body: some View {
            ZStack {
                VStack (alignment: .leading) {
                    VStack {
                        Text("What is your fitness level?")
                            .font(.headline)
                    }
                    HStack (alignment: .top) {
                        Slider(value: self.$fitnessLevel, in: -1...1, step: 0.1)
                    }
                    .frame(height: 50)

                                     // save changes
                                         Rectangle()
                                             .fill( Color.blue )
                                             .frame(height: 150, alignment: .leading)
                                             .overlay(
                                                 Text("Next")
                                                     .font(.largeTitle)
                                                     .fontWeight(.semibold)
                                                     .foregroundColor(.white)
                                         )
                                             .onTapGesture {
                                                self.customerVM.insertCustomerData(userId: self.thisSession.userId!, customerData: CustomerData(fitnessLevel: self.fitnessLevel)) { success in
                                                         if success == true {
                                                            print("FitnessLevel update succeed")
                                                            self.showSurvey.wrappedValue = false
                                                         } else {
                                                            print("FitnessLevel update failed")
                                                         }
                                                     }
                                             }
                    
                    Spacer()
                }
                .padding(.top, 30)
                .frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight)

            }
    }
    
}

MainViewWrapper code, where this view is called from:

struct MainViewWrapper: View {
    @EnvironmentObject var thisSession: CurrentSession
    @ObservedObject var mainData: MainViewModel
    
    // show profile if all data is loaded
    @State var showProfile: Bool = false
    @State var showSurvey: Bool = false
    @State var selection: String? = nil

    init(mainData: MainViewModel) {
        self.mainData = mainData
    }
    
    var body: some View {
        NavigationView {
            ZStack {
                ProfileView()
                .opacity(self.showProfile ? 1 : 0)
                BuildCustomerView(showSurvey: self.$showSurvey)
                .opacity(self.showSurvey ? 1 : 0)  
            }
        }
    }
}

Upvotes: 1

Views: 307

Answers (1)

Asperi
Asperi

Reputation: 258443

Binding as a property (hidden) has _ (underscore), so you have to initialize it as

init(showSurvey: Binding<Bool>) {
    self.customerVM = BuildCustomerViewModel()
    self._showSurvey = showSurvey                // << this !!
}

Upvotes: 1

Related Questions