我在 xcode beta 4 上使用 swiftUI,我看不到对象或属性是否相等。 当我想等同某些东西时,我会在同一行上得到错误:“对成员 '==' 的不明确引用”。
这是我的代码,但在我的代码的其他场合,我也不能等同于枚举等其他东西。这是我的错误还是错误?
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 View 中,错误有时可能会产生误导。它可能出现在代码的一部分,但真正的原因可能是上下几行代码。
在您的情况下,您输入了错误的 View 名称:
PickerView(selection: $data.countDownTime, label: Text("select Time"))
当你真正的意思是:
Picker(selection: $data.countDownTime, label: Text("select Time"))
并且因为 PickerView 实际上确实存在,但具有不同的参数,所以你得到了一个非常具有误导性的错误。
当发生此类事情时,我建议您对给出无意义错误的代码进行注释,然后您会看到编译器会将您指向其他地方。希望是正确的地方。在您的情况下,如果您临时更改:if selector == "countdown"
为 if true
,编译器会为您指明正确的方向。试试看,您就会明白我的意思。
关于swift - 等于或==在swiftui中抛出 "Ambiguous reference"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57231384/