Why am i getting the following error: Extra argument 'selection' in call? It works without the 'selection: $selection' parameter.
XCode Version 11.7
struct AlarmListView: View {
@ObservedObject var alarmModel : AlarmModel
@State var selection: String? = nil
var body: some View {
VStack {
List {
ForEach(alarmModel.alarms, id: \.self, selection: $selection) { alarm in /*error here*/
Text("\(alarm)")
}
}
}
}
}
class AlarmModel: ObservableObject {
@Published var alarms: [String] = []
func addAlarm(_ alarmTime: String) {
alarms.append(alarmTime)
}
func removeAlarm(at index: Int) {
alarms.remove(at: index)
}
}
The syntax is not correct, selection is for List, not ForEach.
Use .onTapGesture instead:
struct AlarmListView: View {
@ObservedObject var alarmModel : AlarmModel
@State var selection: String? = nil
var body: some View {
VStack {
List {
ForEach(alarmModel.alarms, id: \.self) { alarm in /*Change here*/
Text("\(alarm)")
.onTapGesture {
selection = "\(alarm)" // or maybe simply selection = alarm
}
}
}
}
}
}