【问题标题】:Equals or == throws "Ambiguous reference" Error in swiftui等于或 == 在 swiftui 中引发“不明确的引用”错误
【发布时间】:2019-12-05 11:14:26
【问题描述】:

我在 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)
            }
        }
    }
}

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

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

    在您的情况下,您输入了错误的视图名称:

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

    当你真正的意思是:

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

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

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

    【讨论】:

    • 最有趣的是,我刚刚检查了我的代码,它没有显示 PickerView,而是显示了 Picker。但我最后使用了你的提示,它现在在Text("\(exercise)") 处抛出错误表达式类型不明确,没有更多上下文。感谢您的首发!
    猜你喜欢
    • 1970-01-01
    • 2015-01-07
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    相关资源
    最近更新 更多