I am trying to write a reusable selector control.
I want to pass it any enum type where the enum is of the form
enum XXX: String, CaseIterable
and return a binding to the selected enum value. I need to access the raw values of the enum to act as item labels.
i.e., my use case looks something like:
enum Choices: String, CaseIterable {
case first: "First"
case second: "Second"
...
}
I want to build a control view, initialized with a prompt string, an enumeration type that meets the above requirements and a @Binding to an instance of this enum (used as both an initial value and a selection result).
The reusability of this control depends on being able to use it with any suitable enum. (For type safety, it should not compile if the enum is not suitable.)
I can write
struct Selector<E: CaseIterable>: View {
let prompt: String,
let choices: [E]
@Binding var selection: E
init(prompt: String, choices: E.Type, selection: Binding<E>) {
self.prompt = prompt
self.choices = choices.allCases as! [E]
self._selection = selection
}
var body: some View {
...
}
}
but I get a compiler error (Value of type 'E' has no member 'rawValue') when I attempt to access the raw value string.
I don't understand how to express this requirement. Can you help?