There was another post - https://developer.apple.com/forums/thread/651748 asking how to use @FocusedBinding .
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.
Then I define the properties needed to use @FocusedBinding
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
.focusedValue(\.currentDeck, $appState.currentDeck)
Finally, in the struct I try to set the preferences I use it like:
struct AppCommands: Commands {
@FocusedBinding(\.currentDeck) private var currentDeck: DeckViewModel?
}
Then I get the following error:
❌ Type of expression is ambiguous without more context Which makes sense, as the FocusedValues.currentDeck is of type Binding<DeckViewModel?>?
So the syntax of
@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
Thanks
4
0
1.7k