Skip to content

Instantly share code, notes, and snippets.

@zeeshankhan
Created October 23, 2016 05:23
Show Gist options
  • Save zeeshankhan/95d21be5d7a852c18d0248ee6e36065b to your computer and use it in GitHub Desktop.
Save zeeshankhan/95d21be5d7a852c18d0248ee6e36065b to your computer and use it in GitHub Desktop.
Optional value (pattern) matching through enum
import Foundation
let x: String? = nil
let y: String? = "Hello Y"
switch (x,y) {
case (.some(let a), .some(let b)):
print("So \(a) and \(b)")
case (.none, .none):
print("None")
case (.none, .some(let a)):
print("One val \(a)")
default:
print("default")
}
// same can be written as when we need both to be non nil.
switch (x, y) {
case let (a?, b?):
print("So \(a) and \(b)")
default:
print("default")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment