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”