Swiftui 初始化init()中的2个变量:';自我';在初始化所有存储的属性之前使用

Swiftui 初始化init()中的2个变量:';自我';在初始化所有存储的属性之前使用,swiftui,Swiftui,我正在尝试初始化2个变量 self.customerVM = BuildCustomerViewModel() self.showSurvey = showSurvey.wrappedValue 在我的init()函数和xCode中返回 在初始化所有存储属性之前使用“self” 当我尝试只初始化第一个变量而不使用第二个变量时,一切都很顺利。。我不明白为什么 我想知道我应该如何更改代码以使其工作。感谢您的帮助 import SwiftUI struct BuildCustomerView: V

我正在尝试初始化2个变量

self.customerVM = BuildCustomerViewModel()
self.showSurvey = showSurvey.wrappedValue
在我的init()函数和xCode中返回

在初始化所有存储属性之前使用“self”

当我尝试只初始化第一个变量而不使用第二个变量时,一切都很顺利。。我不明白为什么

我想知道我应该如何更改代码以使其工作。感谢您的帮助

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)

            }
    }
    
}
作为属性(隐藏)的绑定具有(下划线),因此必须将其初始化为

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