无法初始化属性和@Binding对象

我也尝试在struct的init()方法中初始化它

struct WorkoutCard: View {

       var numberOfWorkouts : [Int] = [0, 1, 2, 3]

       @State var beginWorkout :Bool = false

 var body: some View {
        ZStack {

       Rectangle().foregroundColor(Color.black)

            if !self.beginWorkout {

    ScrollView (.horizontal, showsIndicators: false) {

         HStack {


   ForEach(self.numberOfWorkouts.reversed(), id: \.self) { index in


           Card(index, beginWorkout: $beginWorkout)


            }
 }

                } }
          }
}


 }


//要初始化的视图

 struct Card: View {

var number : Int

@Binding var beginWorkout : Bool


    init(_ index: Int) {
        self.number = index
          }
  }


错误=调用中有多余的参数“ beginWorkout”

最佳答案

在通话中,您缺少.self

Card(index, beginWorkout: self.$beginWorkout)


而且您的初始化程序缺少您的beginWorkout参数:

struct Card: View {

    var number : Int

    @Binding var beginWorkout : Bool

    init(_ index: Int, beginWorkout: Binding<Bool>) {
        self.number = index
        self._beginWorkout = beginWorkout
    }

    var body: some View {
        ...
    }
}

08-06 13:30