SwiftUI ForEach 理解变量用法
Posted
技术标签:
【中文标题】SwiftUI ForEach 理解变量用法【英文标题】:SwiftUI ForEach understanding variable usage 【发布时间】:2019-11-27 09:53:30 【问题描述】:以下代码对我有用...
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
它停止工作并且无法推断返回类型?
【问题讨论】:
您不能在函数构建器中声明变量,比较***.com/q/56616368/1187415 是否有类似问题。 【参考方案1】:在 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
我明白了:
【讨论】:
以上是关于SwiftUI ForEach 理解变量用法的主要内容,如果未能解决你的问题,请参考以下文章