等于或== throws "Ambiguous reference" swiftui 错误

Equals or == throws "Ambiguous reference" Error in swiftui

我在 xcode beta 4 上使用 swiftUI,但我看不到对象或属性是否相等。 当我想等同于某物时,我会在同一行得到错误:"Ambiguous reference to member '=='".

这是我的代码,但在我的代码的其他场合,我也不能等同于枚举等其他东西。这是我的错误还是错误?

struct PickerView: View {
    @ObjectBinding var data: Model
    //    let selector: PickerType
    let selector: String

    let width: CGFloat
    let height: CGFloat

    var body: some View {
        VStack {
            if selector == "countdown" {
                Picker(selection: $data.countDownTime, label: Text("select Time")) {
                    ForEach(1...240) { diget in
                        Text("\(diget)")
                    }

                }
                .frame(width: width, height: (height/2), alignment: .center)
            } else {
                Picker(selection: $data.exercise, label: Text("select Time")) {
                    ForEach(data.exercises) { exercise in
                        Text("\(exercise)")
                    }

                }
                .frame(width: width, height: (height/2), alignment: .center)
            }
        }
    }
}

在 SwiftUI 视图中,错误有时可能会产生误导。它可能出现在代码的一部分,但真正的原因可能是代码的上下几行。

在你的例子中,你输入了错误的视图名称:

PickerView(selection: $data.countDownTime, label: Text("select Time"))

当你真正的意思是:

Picker(selection: $data.countDownTime, label: Text("select Time"))

并且因为 PickerView 确实存在,但具有不同的参数,所以您得到了一个非常具有误导性的错误。

当发生此类事情时,我建议您对给出无意义错误的代码进行注释,然后您会看到编译器会将您指向其他地方。希望是正确的地方。在您的情况下,如果您暂时将 if selector == "countdown" 更改为 if true,编译器会为您指明正确的方向。试试看,你就会明白我的意思了。