Skip to content

Instantly share code, notes, and snippets.

@carlynorama
Created August 3, 2022 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlynorama/bd8ba233db19370001de12192941d45b to your computer and use it in GitHub Desktop.
Save carlynorama/bd8ba233db19370001de12192941d45b to your computer and use it in GitHub Desktop.
Generates a picker when passed an Emum that conforms to PickerSuppiliable protocol
//
// EnumPicker.swift
// DataViewer
//
// Created by Carlyn Maw on 8/1/22.
//
import SwiftUI
protocol PickerSuppliable:CaseIterable, Hashable {
var menuText:String { get }
static var labelText:String { get }
}
fileprivate enum TestEnum:PickerSuppliable{
static var labelText: String = "TestEnum"
//var id:TestEnum { self }
case yes, no, maybe
var menuText: String {
switch self {
case .yes:
return "Yes"
case .no:
return "No"
case .maybe:
return "Maybe"
}
}
}
struct EnumPicker<E:PickerSuppliable>: View {
let options:[E] = E.allCases as! [E]
var label:String? = nil
@Binding var value:E
var body: some View {
VStack {
VStack {
Picker(label ?? E.labelText, selection: $value) {
//ForEach(options) { option in is only for identifiable
ForEach(options, id: \.self) { option in
Text(option.menuText)
}
}
}
}
}
}
struct EnumPicker_Previews: PreviewProvider {
static var previews: some View {
EnumPicker<TestEnum>(value: .constant(.maybe))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment