Search code examples
swiftuipickerobservedobject

Can a published var in observed object be used directly in Picker selection as binding var?


I am new to xcode 11 and SwiftUI. I am working on a settings view for my app. Created an ObservableObject with Published var dataType Int. In the settings view, i have a picker view where i pass in the settingsStore.dataType. It gives me below error.

Cannot convert value of type 'Int' to expected argument type
'Binding<SelectionValue>'

I know i can work around this by setting a @State var dataType in the view and passing in $dataTpye for the Picker and then assign the value to the settings object. Just wondering if there is a more straight forward method of doing it.

    import SwiftUI
    import Combine

    struct SettingsView: View {

        @ObservedObject var settingsStore: SettingsStore

        var body: some View {
            VStack {
                Text("Settings").font(.title)

                Text("Number of weeks in calendar view")
                HStack (spacing: 28) {
                    Button (action: {
                        self.settingsStore.numberOfWeeks -= 1
                    }) {
                        Image(systemName: "minus.circle")
                            .resizable()
                            .frame(width:60, height:60)
                    }

                    Text("\(settingsStore.numberOfWeeks)")
                        .font(.system(size: 38.0))

                    Button (action: {
                        self.settingsStore.numberOfWeeks += 1
                    }) {
                        Image(systemName: "plus.circle")
                            .resizable()
                            .frame(width:60, height:60)
                    }
                }

                Text("Default data type")
                Picker(selection: settingsStore.dataType, label: Text("Dafault Data Type")) {
                    Text("Blood Ketone Value (mmol/L)").tag(0)
                    Text("Ketostix").tag(1)
                }.pickerStyle(SegmentedPickerStyle())
            }
        }
    }

    class SettingsStore: ObservableObject {

        @Published var numberOfWeeks: Int {
            didSet {
                    UserDefaults.standard.set(numberOfWeeks, forKey: "numberOfWeeks")
                }
            }

        @Published var dataType: Int {
            didSet {
                    UserDefaults.standard.set(dataType, forKey: "dataType")
                }
            }

        init() {
            self.numberOfWeeks = UserDefaults.standard.integer(forKey: "numberOfWeeks")
            self.dataType = UserDefaults.standard.integer(forKey: "dataType")
        }
    }


    struct SettingsView_Previews: PreviewProvider {
        static var previews: some View {
            return SettingsView(settingsStore: SettingsStore())
        }
    }

Solution

  • The same notation, ie $, for ObservedObject properties,

    Picker(selection: $settingsStore.dataType, label: Text("Dafault Data Type")) {
        Text("Blood Ketone Value (mmol/L)").tag(0)
        Text("Ketostix").tag(1)
    }.pickerStyle(SegmentedPickerStyle())