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

Swift:无法推断复杂的关闭返回类型;添加显式类型以消除歧义,swift,swiftui,Swift,Swiftui,根据错误,您需要帮助编译器计算返回类型,所以second->Text?在中而不是在中第二次尝试 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,

根据错误,您需要帮助编译器计算返回类型,所以
second->Text?在
中而不是在中第二次尝试

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

看看其他答案。添加组很好

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

我得到了它。在第二个foreach中,它必须有一些回报。谢谢你的回答
let digits=[1,2,3,4,5,6,7,8,9]
Swift将推断类型,无需编写它,或缩短
let digits=(1…9)
Sh Khan谢谢您的回答
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)")
                    }
                }
            }
        }
    }
}