【发布时间】:2019-06-16 03:52:04
【问题描述】:
当我尝试在字典的值循环内设置显示切换时,我从错误消息中得到的帮助很少。
如果我取消注释下面的 3 行注释代码,尝试为循环中的每个属性添加切换,我会收到以下错误:
无法将 'HStack, Text, ConditionalContent)>>' 类型的值转换为闭包结果类型 '_'
import SwiftUI
struct properties {
var id : Int
var property : String
var isOn : Bool
}
struct ContentView: View {
@State var propertyValues: [properties] = [properties(id: 1, property: "DemoData", isOn: true),
properties(id: 2, property: "ShowLocalEvents", isOn: false)]
var body: some View {
NavigationView {
VStack {
List {
ForEach(propertyValues.identified(by: \.id)) { propertyValue in
HStack {
// Toggle(isOn: propertyValue.isOn) {
// Text("")
// }
Text("\(propertyValue.property)")
if propertyValue.isOn {
Text("On")
} else {
Text("Off")
}
}
}
}
}
}
}
}
【问题讨论】: