Search code examples
swiftuibindingpicker

SwiftUI @State weird wrappedValue behaviour


So straight to the point i have this code:

struct DataModel {
    var option: String

}

struct ViewModel {
    var selected: String? = "1"
    var options = [DataModel(option: "1"), DataModel(option: "2"), DataModel(option: "3")]
}

struct ContentView: View {
    
    @State var viewModel: ViewModel
    
    var body: some View {
        VStack {
            Picker("test", selection: aBinder()) {
                ForEach(viewModel.options, id: \.option) { option in
                    Text(option.option)
                }
            }
            .background(Color.red)
        }
    }
    
    func aBinder() -> Binding<String?> {
        Binding<String?> {
            viewModel.selected
        } set: { value in
            $viewModel.selected.wrappedValue = value
            print($viewModel.selected.wrappedValue)
        }
    }
}

The value of "selected" in the viewModel doesn't change.

This works:

struct DataModel {
    var option: String
}

struct ViewModel {
    var selected: String = "1"
    var options = [DataModel(option: "1"), DataModel(option: "2"), DataModel(option: "3")]
}

struct ContentView: View {
    
    @State var viewModel: ViewModel
    
    var body: some View {
        VStack {
            Picker("test", selection: $viewModel.selected) {
                ForEach(viewModel.options, id: \.option) { option in
                    Text(option.option)
                }
            }
            
            Button("press me", action: { print(viewModel.selected) })
        }
    }
}

But that doesn't make any sense. in both cases i use a binding to store the current value. What is going on? I'm pretty new to swiftUI so i might have missed how something works.

Thanks in advance


Solution

  • The types don’t match selected is a String and the options are a DataModel. The types have to match exactly.