I'm trying to use @FocusedBinding in the following context.
I have a view that contains a @State array property of type [DeckViewModel]
Within the view, I display the list of those models
I can tap on one item to select it, storing that selection in a @State var currentDeck: DeckViewModel? property.
Code Block swift struct FocusedDeckKey : FocusedValueKey { typealias Value = Binding<DeckViewModel?> } extension FocusedValues { var currentDeck: FocusedDeckKey.Value? { get { self[FocusedDeckKey.self] } set { self[FocusedDeckKey.self] = newValue } } }
And set the value in the view
Code Block swift .focusedValue(\.currentDeck, $appState.currentDeck)
Finally, in the struct I try to set the preferences I use it like:
Code Block swift struct AppCommands: Commands { @FocusedBinding(\.currentDeck) private var currentDeck: DeckViewModel? }
Then I get the following error:
Which makes sense, as the FocusedValues.currentDeck is of type Binding<DeckViewModel?>?❌ Type of expression is ambiguous without more context
So the syntax of
Code Block @FocusedBinding(\.currentDeck) private var currentDeck: DeckViewModel?
is not correct for these types. But I am not sure how to express this correctly.
The idea of the binding to an optional value seems ok to me, but it brings this complexity regarding the types and this API.
Then.
How can I make @FocusedBinding work with this optional?
or How should I solve this issue? considering I'm trying to represent the selection of an item of a list