Skip to content

Instantly share code, notes, and snippets.

@SimplGy
Last active July 19, 2016 18:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimplGy/acc81523ceeef1f364d5148fa8e5d68e to your computer and use it in GitHub Desktop.
Save SimplGy/acc81523ceeef1f364d5148fa8e5d68e to your computer and use it in GitHub Desktop.
Switching on optional enums
enum Coin {
case heads
case tails
}
var result: Coin?
// You can pattern match against it like an optional:
switch result {
case .heads?: print("heads")
case .tails?: print("tails")
case nil: print("not yet flipped") // exhaustive
}
// Or, you can pattern match it agains the the optional enum that wraps all optional types:
switch result {
case .Some(.heads): print("heads")
case .Some(.tails): print("tails")
case .None: print("not yet flipped") // also exhaustive
}
// This even works on optional booleans
switch tab?.paid {
case true?:
tabState = .paid
case false?:
tabState = .open
case nil:
tabState = .empty
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment