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

I'm very thankful for @pdarcey for providing the solution:
https://infosec.exchange/@pdarcey/111974535950798737

Indeed @ Bindable would make me achieve the desired result: https://developer.apple.com/documentation/swiftui/bindable

Sadly, I cannot target iOS 17 yet – which by the way is a true pain with #SwiftUI – so that’s not an option for now, but it's great to know that I'll be able to get rid of all those intermediary variables later!

Paul Darcey (@[email protected])

@[email protected] Could you use @Bindable on decodedObject? (I.e. is decodedObject Observable?) https://developer.apple.com/documentation/swiftui/bindable

Infosec Exchange

Also, kudos to whomever wrote the @ Bindable documentation at Apple.

Succinct, and yet extremely clear!

Just the first code example brings home the concept, it's such a pain I can't use it 🥺

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
@cdf1982 Struggles like this are quite normal. And frustrating! I don’t have a good solution for you but I basically just wanted to say “me too”
@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!