Skip to content

Instantly share code, notes, and snippets.

@mlvea
Last active August 29, 2015 14:17
Show Gist options
  • Save mlvea/166c4026e6a7d03d5508 to your computer and use it in GitHub Desktop.
Save mlvea/166c4026e6a7d03d5508 to your computer and use it in GitHub Desktop.
Optionals and Dictionary
let netWorthOfBillionaires = ["Bill Gates":78.5,"Carlos Slim Helu":71,"Warren Buffett":70.9]
let possibleNetWorth = netWorthOfBillionaires["Tim Cook"]
//Type of the possibleNetWorth is inferred to Double?
//Know what! Tim cook is not a billionair yet. His net worth is aroung $400 million
//Thats why he is not in our list. But who knows. He might becomes a billionair
if (possibleNetWorth != nil){
let netWorth = possibleNetWorth!
// ! Force the value out of optional
println("Tim Cook's net worth in billions : \(netWorth)")
}
// But this is so common swift has more eligent way to do this
if let netWorth = possibleNetWorth {
println("Tim Cook's net worth in billions : \(netWorth)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment