Accessing ViewModel field in SwiftUI using Xcode 12: “Accessing State’s value outside of being installed on a View”

Thanks to @Andrew’s answer I figured out how to make it work again. First you change the @State to @Published:

@Published public var amount: Int = 1

Next, you need to change how your Picker is bound to the data:

Picker(selection: $model.amount, label: Text("Amount")) {
    Text("€1").tag(1)
    Text("€2").tag(2)
    Text("€5").tag(5)
    Text("€10").tag(10)
}.pickerStyle(SegmentedPickerStyle())

So we went from model.$amount to $model.amount.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)