Post

Replies

Boosts

Views

Activity

SwiftUI - Using Bindable with Environment
At the moment, using Bindable for an object stored in Environment works in a cumbersome way: struct ContentView: View { @Environment(Model.self) var model var body: some View { @Bindable var model = model VStack { Text(model.someField.uppercased()) TextField("", text: $model.someField) someSubView } .padding() } @ViewBuilder var someSubView: some View { @Bindable var model = model TextField("", text: $model.someField) } } A new @Bindable needs to be instantiated for each computed property in the view, which creates boilerplate I would like to avoid. I made a new property wrapper which functions the same as the EnvironmentObject wrapper, but for Observable: @propertyWrapper struct EnvironmentObservable<Value: AnyObject & Observable>: DynamicProperty { @Environment var wrappedValue: Value public init(_ objectType: Value.Type) { _wrappedValue = .init(objectType) } public init() { _wrappedValue = .init(Value.self) } private var store: Bindable<Value>! var projectedValue: Bindable<Value> { store } mutating func update() { store = Bindable(wrappedValue) } } Example: struct ContentView: View { @EnvironmentObservable var model: Model var body: some View { VStack { Text(model.someField.uppercased()) SubView(value: $model.someField) someSubView } .padding() } var someSubView: some View { TextField("", text: $model.someField) } } I was wondering if there would be any downsides to using this method? In my testings it seems to behave the same, but I'm not sure if using this could have a performance impact.
1
2
735
Sep ’23