【问题标题】:Issues when initialising @Binding with other properties使用其他属性初始化 @Binding 时的问题
【发布时间】:2019-08-18 21:34:03
【问题描述】:

无法连同@Binding 对象一起初始化属性

我也尝试在结构体的 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”

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    在您的通话中,您缺少.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 {
            ...
        }
    }
    

    【讨论】:

    • 谢谢!!,我没有添加 Binding 参数。在我之前的尝试中,我只添加了 Bool 作为类型。现在说得通了。
    猜你喜欢
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2021-09-26
    • 2019-11-03
    • 2020-11-10
    • 1970-01-01
    相关资源
    最近更新 更多