Im trying to write an extension for an enum that is CaseIterable
so that i can get an array of the raw values instead of the cases, Im not entirely sure how to do that though
extension CaseIterable {
static var allValues: [String] {
get {
return allCases.map({ option -> String in
return option.rawValue
})
}
}
}
i need to add a where clause somehow, if i dont have a where clause i get an error saying 'map' produces '[T]', not the expected contextual result type '[String]'
Anyone know if there is a good way of going about this?
my enums i want this function to work on look a bit like this
enum TypeOptions: String, CaseIterable {
case All = "all"
case Article = "article"
case Show = "show"
}
Not all enumeration types have an associated
RawValue
, and if they have then it is not necessarily aString
.Therefore you need to restrict the extension to enumeration types which are
RawRepresentable
, and define the return value as an array ofRawValue
:Examples: