Hi,
Let's say that I have such a simple reducer:
struct OneOfManySelection: ReducerProtocol {
struct State {
let ints: [Int]
var selected: Int?
}
enum Action {
case select(id: Int)
}
var body: some ReducerProtocol<State, Action> {
...
}
}
The logic, that I'm after is simple - whenever the select
action gets sent I want to set the given id
as selected
. I'm tempted to use a forEach
here and model the ElementState
as:
struct SingleSelectionState {
let int: Int
var selected: Int?
}
This is problematic though. In order to wire it up I'd have to come up with a computed property, here's a possible solution:
var _ints: IdentifiedArray<Int, SingleSelection.State> {
get {
IdentifiedArray(
uniqueElements: ints.map {
SingleSelectionState(int: $0, selected: selected)
},
id: \.int
)
}
set {
selected = newValue.first(where: { $0.selected != selected })?.int
}
}
The get
part is not so bad, but the set
is a bit nasty. It works, but meh... Doesn't feel right.
Are there any other options available to use forEach
and leverage its niceness without having to go with such a computed property? Or is forEach
not the right tool at all for a situation where some non-exclusive ElementState
(the selected
part) is involved?