1

With the new @Observable macro introduced for iOS 17, we can now use environment objects in the following way

@Environment(MyType.self) var myTypeObj

Now if myTypeObj has some property, call it myProperty which I'd like to pass as binding somewhere using $ syntax, Swift complains that it cannot find the variable. For example,

Picker("My Picker", selection: $myTypeObj.myProperty) { // we get an error on this line
  ForEach(1 ... 50, id: \.self) { num in
    ...
  }
}

we get an error saying Cannot find $myTypeObj in scope. Is this a Swift bug or am I doing something wrong?

Nicolas Gimelli
  • 695
  • 7
  • 19

1 Answers1

2

In iOS 17 you have to use Bindable.

@Observable
class AppState {
    var isOn: Bool = false
}

struct ContentView: View {
    
    @Environment(AppState.self) private var appState
    
    var body: some View {
        
        @Bindable var bindable = appState
        
        VStack {
            Toggle("Is On", isOn: $bindable.isOn)
        }
        .padding()
    }
}

#Preview {
    ContentView()
        .environment(AppState())
}
azamsharp
  • 19,710
  • 36
  • 144
  • 222