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()
}