Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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