Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind environment variable ios17

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?

like image 730
Nicolas Gimelli Avatar asked Sep 02 '26 17:09

Nicolas Gimelli


2 Answers

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

in main App:

import SwiftUI

@main
struct myApp: App {

    private var appState = AppState()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(appState)

        }
    }
}
like image 192
azamsharp Avatar answered Sep 04 '26 11:09

azamsharp


To myself and others I also think azamsharp in his video enter link description here mentioned for Binding to work we have to use a separate view to bind to. But here we can see that there are another way:

  1. @Environment(AppState.self) private var appState
  2. @Bindable var bindable = appState but most important :this @Bindable is placed in "body View"
like image 40
Paragate Avatar answered Sep 04 '26 12:09

Paragate



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!