ios – Does anybody know why Picker from my view file will not settle for the binding model of an observable object property utilizing @Observable

ios – Does anybody know why Picker from my view file will not settle for the binding model of an observable object property utilizing @Observable


I’m not complete certain why utilizing the @Observable tag doesn’t work the identical as utilizing the ‘previous’ manner which is utilizing @Printed and ObservableObject and @StateObject. I’m simply curious why that would not work since utilizing @Observable is meant to be the brand new manner.

Additionally I’m new to SwiftUI.
I’m making an attempt to get the Picker from the view display screen to replace the variable currentSelection from SettingsViewModel on any consumer interactions with the picker.

import Basis

@Observable
class SettingsViewModel {
    
    var currentSelection : ColorOptionEnum
    var dataHandler : DataHandler
    
    init() {
        let localdataHandler: DataHandler = .init()
        currentSelection = localdataHandler.currentBibleVersion
        dataHandler = localdataHandler
    }
}

-------------

import SwiftUI

struct SettingsView: View {
    var settingsViewModel : SettingsViewModel = .init()

    
    var physique: some View {

        Kind {
            Textual content("Mode")
            Picker(
                "Mode",
                choice: $settingsViewModel.currentSelection // GETTING Error right here ( can not discover '$settingsViewModel' in scope)
            ) {
                Textual content(
                    "Inexperienced"
                )
                .tag(
                    ColorOptionEnum.Inexperienced
                )
                Textual content(
                    "Blue"
                )
                .tag(
                    ColorOptionEnum.Blue                )

                            }
            
        }
    }
}

#Preview {
    SettingsView()
}

This model works

import Basis

class SettingsViewModel: ObservableObject {
    
    @Printed var currentSelection : ColorOptionEnum
    var dataHandler : DataHandler
    
    init() {
        let localdataHandler: DataHandler = .init()
        currentSelection = localdataHandler.currentBibleVersion
        dataHandler = localdataHandler
    }
}

-------------

import SwiftUI

struct SettingsView: View {
    @StateObject var settingsViewModel : SettingsViewModel = .init()

    
    var physique: some View {

        Kind {
            Textual content("Mode")
            Picker(
                "Mode",
                choice: $settingsViewModel.currentSelection // No error
            ) {
                Textual content(
                    "Inexperienced"
                )
                .tag(
                    ColorOptionEnum.Inexperienced
                )
                Textual content(
                    "Blue"
                )
                .tag(
                    ColorOptionEnum.Blue             
                )

            }
            
        }
    }
}

#Preview {
    SettingsView()
}

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.