SwiftUI ForEach了解变量用法

时间:2019-11-27 09:53:30

标签: swift swiftui

为什么此代码可以正常工作

VStack {
                    ForEach(0..<self.rows, id: \.self) { row in
                        HStack {
                            ForEach(0..<self.columns, id: \.self) { column in

                                //let data = self.item(row: row, column: column)

                                Group {
                                    if self.item(row: row, column: column) != nil {

                                        self.content(self.item(row: row, column: column)!).frame(width: geometry.size.width/CGFloat(self.columns))

                                    } else {
                                        Spacer()
                                    }
                                }

                            }
                        }
                    }

但是,如果我取消注释data变量并用数据使用量替换了item()调用,甚至添加了return Group { },它将停止工作并且无法推断返回类型?

1 个答案:

答案 0 :(得分:0)

在Xcode 12 beta中,只需将类型添加到参数中即可。

我的示例代码: (我更喜欢把SwiftUi的东西分开。否则太乱了。。) 查看附近的颜色

struct Issue: Identifiable{
        var id  = UUID()
        var name = ""
        var fakeMsg = ""
        var operatorInCharge = ""
        var solved = false
    }
    
    

struct CurrentIssuesView: View {
    
    let roomIssues = [
        Issue(name: "ROOM 1", fakeMsg: "2020-09-05 proiettore", operatorInCharge: "m.a", solved: true),
        Issue(name: "ROOM 1", fakeMsg: "2020-09-04 wifi", operatorInCharge: "a.b"),
        Issue(name: "ROOM 2", fakeMsg: "2020-09-04 Audio", operatorInCharge: "m.c", solved: true),
        Issue(name: "ROOM 3", fakeMsg: "2020-09-04 KBD", operatorInCharge: "m.d")
    ]
    
    
    var body: some View {
        
        let vs = VStack{
            
            Text("Current ISSUES")
            
            List(roomIssues) { (issue: Issue) in
                
                let color = issue.solved ? Color.green : Color.red
                HStack{
                    Text(issue.name).font(.system(size: 20)).foregroundColor(color)
                    Text(issue.fakeMsg)
                }
            }
        }
        return vs
    }
}

我知道了

enter image description here