Skip to content

Instantly share code, notes, and snippets.

@oisdk
Created December 28, 2015 01:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oisdk/4c40958d341952fde5af to your computer and use it in GitHub Desktop.
Save oisdk/4c40958d341952fde5af to your computer and use it in GitHub Desktop.
let dict = [1:"a", 2:"b"]
let key: Int? = 2
if let value = key.map({ dict[$0] }) {
print(value) // "Optional("b")"
}
if let key = key, value = dict[key] {
print(value) // "b"
}
if let value = key.flatMap({dict[$0]}) {
print(value) // "b"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment