Swift:无法推断复杂闭包返回类型;添加显式类型以消除歧义


struct _9table: View {
let digits: [Int] = [1,2,3,4,5,6,7,8,9]
var body: some View {
VStack{
ForEach(self.digits, id: .self) { first in
ForEach(self.digits, id: .self) { second in
if second > first {
Text("(first)+(second)=(first+second)")
}
}
}
}
}
}

根据错误,您需要帮助编译器计算返回类型,所以请执行second -> Text? in而不是second in尝试

struct _9table: View {
let digits = (1...9)
var body: some View {
VStack{
ForEach(self.digits, id: .self) { first in
ForEach(self.digits, id: .self) { second -> Text? in
if second > first {
return Text("(first)+(second)=(first+second)")
}
return nil
}
}
}
}
}

看其他答案后。Add Group的很好

struct _9table: View {
let digits = (1...9)
var body: some View {
VStack{
ForEach(self.digits, id: .self) { first in
ForEach(self.digits, id: .self) { second in
Group{
if second > first {
return Text("(first)+(second)=(first+second)")
}
}
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新