Created
October 23, 2016 05:23
-
-
Save zeeshankhan/95d21be5d7a852c18d0248ee6e36065b to your computer and use it in GitHub Desktop.
Optional value (pattern) matching through enum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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