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)")
}
}
}
}
}
}