I'm trying to write an extension that extends enums based on strings. The way I know to extend all enumerations is to extend RawRepresentable, but I want it restricted to strings-only.
extension RawRepresentable where RawRepresentable.RawValue == String{
func foo(){
let myRawValue:String = self.rawValue
}
}
So how do you specify a 'where' clause to achieve this?
To extend just RawRepresentable
s based on String
s, the where
clause is simply where RawValue == String
:
extension RawRepresentable where RawValue == String {
func foo() {
let myRawValue:String = self.rawValue
print(myRawValue)
}
}
enum Flintstone: String {
case fred, wilma, pebbles
}
Flintstone.fred.foo() // fred