We have the practice of using safe subscript when accessing any element in a collection. Below is the extension we have.
extension Collection {
subscript(safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}
But when I try to use this with a binding object, it gives me an error saying
Extraneous argument label 'safe:' in subscript
Below is the problematic code
struct MyView: View {
@ObservedObject var service: service
var body: some View {
List {
ForEach(service.items.indices) { index in
Toggle(isOn: self.$service.items[safe: index]?.isOn ?? false) { // Error: Extraneous argument label 'safe:' in subscript
Text("isOn")
}
}
}
}
}