In AppKit/UIKit, when I get an object from a REST API I decode it, modify it, and then push the modified object back.

REST ➡️ decodedObject ➡️ decodedObject.foo = true ➡️ REST

I struggle to keep it as simple with #SwiftUI!

Let’s say I want to use a Toggle to flip decodedObject.foo, I can't because it expects a Binding, so I end up with

REST ➡️ decodedObject ➡️ .onLoad { (@ State var) fooCopy = decodedObject.foo ➡️ Toggle(isOn: $fooCopy) } ➡️ .onDisappear { decodedObject.foo = fooCopy } ➡️ REST

@cdf1982 Could you use @Bindable on decodedObject? (I.e. is decodedObject Observable?) https://developer.apple.com/documentation/swiftui/bindable
Bindable | Apple Developer Documentation

A property wrapper type that supports creating bindings to the mutable properties of observable objects.

Apple Developer Documentation

@pdarcey First of all, thank you!

I am not very experienced with SwiftUI, so I've only occasionally heard about @ Bindable, but reading the docs you linked to makes it clear that it is exactly what I was looking for.

I was very excited by the fact that my decodingObject was already conforming to Identifiable, but sadly making it @ Observable is not yet an option for me, because I cannot target iOS 17 yet 😭.

Still, I am happy to know that the copy variables are only a stopgap.

Thanks again!

@cdf1982 @pdarcey If you can’t use Observable, use ObservableObject. It’s the slightly less convenient predecessor.
@helge Thank you, I'll investigate this!
@cdf1982 I tend to do it in #SwiftUI more or less exactly like in AppKit/UIKit. A controller (the Observable or ObservableObject w/ Published), the View for presentation and then your models that are managed/coordinated by the controller.

@cdf1982 @pdarcey maybe this would be a good solution to use in the meantime while you still support older iOS version? It’s a backport of the Observation framework introduced in iOS 17.

https://github.com/pointfreeco/swift-perception

GitHub - pointfreeco/swift-perception: Observable tools, backported.

Observable tools, backported. Contribute to pointfreeco/swift-perception development by creating an account on GitHub.

GitHub
@JTostitos Yes! It seems a brilliant solution in the meantime, thanks!