I want to return an enum from my function. For that, I need to classify a return type.
However, what is the return type if I want to return an enum?
My code below:
class Timezone {
func getTimeZoneFromCountry(country: String) -> ??? {
switch country {
case "AU":
return Timezone.AU
case "US":
return Timezone.US
default:
return Timezone.AU
}
}
enum AU: String, Identifiable, CaseIterable {
case AEST = "AEST"
case AWST = "AWST"
case ACST = "ACST"
var id: AU {self}
}
enum US: String, Identifiable, CaseIterable {
case CST = "CST"
case EST = "EST"
case MST = "MST"
case PST = "PST"
var id: US {self}
}
}
I'm trying to dynamically populate a Picker:
struct TimeZonePicker: View {
@Binding var country: String // AU or US
var body: some View {
Picker("Timezone", selection: $country) {
ForEach(*Dynamic enum here*) { i in
Text(String(i.rawValue))
}
}
}
}
Edit: New view with @Sweeper's dynamic solution:
var body: some View {
Picker("Timezone", selection: $country) {
ForEach(Timezone.getTimeZoneFromCountry(country: country)) { i in
Text(String(i.rawValue))
}
}
}