Im收到以下错误编译器无法在合理的时间内对此表达式进行类型检查;尝试将表达式拆分为不同的子表达式
尝试创建自定义号码板时
struct CustomNumberPad: View {
@Binding var value: String
var isVerify: Bool
// Number Pad
var rows = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "", "0", "delete.left"]
var body: some View {
GeometryReader { reader in
VStack {
LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 20), count: 3), spacing: 15) {
ForEach(rows, id: \.self) { value in
Button(action: { buttonAction(value: value) }) {
ZStack {
if value == "delete.left" {
Image(systemName: value)
.font(.title)
.foregroundColor(.black)
} else {
Text(value)
.font(.title)
.fontWeight(.bold)
.foregroundColor(.black)
}
}
.frame(width: getWidth(frame: reader.frame(in: .global)), height: getHeight(frame: reader.frame(in: .global)))
.background(Color.white)
.cornerRadius(10)
}
.disable(value == "" ? true : false)
}
}
}
}
.padding()
}
func getWidth(frame: CGRect) -> CGFloat {
let width = frame.width
let actualWidth = width - 40
return actualWidth / 3
}
func getHeight(frame: CGRect) -> CGFloat {
let height = frame.height
let actualHeight = height - 40
return actualHeight / 4
}
func buttonAction(value: String) {
if value == "delete.left" && self.value != "" {
self.value.removeLast()
}
if value != "delete.left" {
if isVerify {
if self.value.count < 6 {
self.value.append(value)
}
} else {
self.value.append(value)
}
}
}
}
有人能建议我怎么解决这件事吗?!
以下是一些您可以自己修复编译器无法在合理时间对此表达式进行类型检查
的方法:
https://stackoverflow.com/A/67025833/11837341
在您的例子中,您使用了错误的修饰符:
// Doesn't Work
.disable(value == "" ? true : false)
我们没有.disable()
函数,该函数的正确名称是.disable()
:
// Works
.disabled(value == "" ? true : false)