christianselig,
@christianselig@mastodon.social avatar

Small addendum to my previous code question, how do I use an @​Observable as a binding if the object I want to bind is optional? In this case, the object might not be immediately available as it comes in async, but once it comes in, I want it selectable

This code compiles, but the selection is never saved

There's probably a better way to structure this too, in that case I'm all ears!

Gist: https://gist.github.com/christianselig/792257d1270f0c6dd3ebd9fde062ed51

dinesharjani,
@dinesharjani@hachyderm.io avatar

@christianselig say Christian, where can I steal your Xcode color theme from? :D

christianselig,
@christianselig@mastodon.social avatar

@dinesharjani It's called One Dark! I don't quite remember where I got it from but it looks like a popular one at least

fruitcoder,

@christianselig can you if let the Binding using the unwrap initializer and only the show the Picker?

pakkito,

@christianselig I think you need to add @Bindable

layoutSubviews,
@layoutSubviews@mastodon.social avatar

@christianselig FYI the .tag() modifier is superfluous here — it provides the same identity as the ForEach() already provides

christianselig,
@christianselig@mastodon.social avatar

@layoutSubviews If I drop it though, it no longer works :(

layoutSubviews,
@layoutSubviews@mastodon.social avatar

@christianselig ah, right, because of the outer if let. My bad.

christianselig,
@christianselig@mastodon.social avatar

@layoutSubviews Oh all good, I'm appreciative that I now understand why it's needed :p

mariani1,
@mariani1@mastodon.social avatar

@christianselig @layoutSubviews Wait, I don’t understand! Please explain. 😊

maxmelzer,
@maxmelzer@mastodon.social avatar

@christianselig Are you aware of „Bindable“? You can also define a Bindable var inside your body callback to access stuff from the environment. https://developer.apple.com/documentation/swiftui/bindable

christianselig,
@christianselig@mastodon.social avatar

@maxmelzer How would I use it in this situation? I've tried a few different incantations to no avail

maxmelzer,
@maxmelzer@mastodon.social avatar

@christianselig Oooh, I think I figured it out. The problem is not about the Observable at all. The trick is that the type of your .tag must be exactly the type of your selection binding, including optionality. Try using „.tag(iceCream as String?)“, that should do the trick.

maxmelzer,
@maxmelzer@mastodon.social avatar

@christianselig Ah, sorry, I see you already got that info. Thanks for the small puzzle, though! :)

zachwaugh, (edited )
@zachwaugh@mastodon.social avatar

@christianselig I think this may be an issue of the type of the .tag being String instead of String?, which you can fix by doing .tag(Optional(iceCream)) or casting it to String?

christianselig,
@christianselig@mastodon.social avatar

@zachwaugh Aah that worked a charm, thank you!

bwebster,
@bwebster@mastodon.social avatar
christianselig,
@christianselig@mastodon.social avatar

@bwebster I think I understand Bindable in the context of initializing a new object with the Bindable object, but how would I use it here?

bwebster,
@bwebster@mastodon.social avatar

@christianselig I think you should be able to just replace @State with @Bindable in that example? (not 100% sure as I've only used it a couple times myself)

christianselig,
@christianselig@mastodon.social avatar

@bwebster No dice :(

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • rosin
  • cisconetworking
  • GTA5RPClips
  • osvaldo12
  • khanakhh
  • DreamBathrooms
  • magazineikmin
  • Youngstown
  • everett
  • mdbf
  • slotface
  • InstantRegret
  • kavyap
  • cubers
  • megavids
  • modclub
  • normalnudes
  • Durango
  • thenastyranch
  • ethstaker
  • tacticalgear
  • ngwrru68w68
  • Leos
  • anitta
  • provamag3
  • tester
  • JUstTest
  • lostlight
  • All magazines