ForEach 和 SwiftUI 的索引不断提供"Closure containing control flow statement cannot be used with result build



我正在尝试创建一个ForEach循环,用于查找当前所选选择器值的索引。但我不断收到这些错误消息:CCD_ 1,CCD_ 2,Closure containing control flow statement cannot be used with result builder 'TableRowBuilder'

@State private var loc: Double = 0
@State private var selectedLine = "N Q R W"
Picker("Lines", selection: $selectedLine) {
ForEach(station.groups, id: .self) {
Text($0)
}
}
.padding(.all, 6.0)
.pickerStyle(.segmented)
ForEach (0 ..< station.groups.count, id: .self) { i in //first 2 errors here
Text("Value: (station.groups[i])")
if station.groups[i] == selectedLine { //second error
loc = station.groups.index(of: selectedLine)
}
}

station.group是JSON文件中的非静态变量。例如["N Q R W","one two three"]["A C E","B D F M"]非常感谢。

编辑:我找到了一个变通办法。

Picker("Lines", selection: $selectedLine) {
ForEach(0..<station.groups.count, id: .self) { i in
Text(station.groups[i])
}
}
.padding(.all, 6.0)
.pickerStyle(.segmented)

selectedLineInt,现在它输出0,1,2,&3而不是数组值。

ForEach是一个Referencing initializer 'init(_:id:content:)' on 'ForEach' requires that 'Text' conform to 'TableRowContent'0,它不是for循环,不能使用.selfarray.count,必须提供一个作为数据唯一标识符的键,或者让数据结构实现Identifiable,并从JSON数据中设置let id

相关内容

最新更新