Binding SwiftUI Picker to value held in dictionary

Viewed 1095

The follow code compiles and runs, but only the pickers bound to values in the array work. Those held in a dictionary fail. Any ideas why this maybe the case?

import SwiftUI

struct ContentView: View {

    private var options : [String] = ["Bonjour", "Hello", "Hola"]

    @State private var choicesHeldInArray = [0, 2]
    @State private var choicesHeldInDict: [String: Int] = ["C3": 2, "C4":1]

    var body: some View {

        VStack {

            Picker(selection: $choicesHeldInArray[0], label: Text("Choice 1")) {
                ForEach(0 ..< options.count) {
                    Text(self.options[$0])
                }
            }

            Picker(selection: $choicesHeldInArray[1], label: Text("Choice 2")) {
                ForEach(0 ..< options.count) {
                    Text(self.options[$0])
                }
            }

            Picker(selection: $choicesHeldInDict["C3"], label: Text("Choice 3")) {
                ForEach(0 ..< options.count) {
                    Text(self.options[$0])
                }
            }

            Picker(selection: $choicesHeldInDict["C4"], label: Text("Choice 4")) {
                ForEach(0 ..< options.count) {
                    Text(self.options[$0])
                }
            }
        }
    }
}
1 Answers
Related