Skip to content

Instantly share code, notes, and snippets.

@ManWithBear
Forked from jckarter/gist:53fcd4046e2857bd315b
Last active November 14, 2016 11:32
Show Gist options
  • Save ManWithBear/cd482b0b839eb8e583328379accc989f to your computer and use it in GitHub Desktop.
Save ManWithBear/cd482b0b839eb8e583328379accc989f 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
case 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