Skip to content

Instantly share code, notes, and snippets.

@SimplGy SimplGy/optionalEnum.swift
Last active Jul 19, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.