Skip to content

Instantly share code, notes, and snippets.

@jckarter
Created October 25, 2014 19:14
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jckarter/53fcd4046e2857bd315b to your computer and use it in GitHub Desktop.
Save jckarter/53fcd4046e2857bd315b to your computer and use it in GitHub Desktop.
Optional chaining raw value conversion
extension String {
func toEnum<Enum: RawRepresentable where Enum.RawValue == String>() -> Enum? {
return Enum(rawValue: self)
}
}
enum Segue: String {
case Foo = "Foo"
case Bar = "Bar"
}
let x: String? = "Foo"
let y: Segue? = x?.toEnum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment