Last active
August 29, 2015 14:17
-
-
Save mlvea/166c4026e6a7d03d5508 to your computer and use it in GitHub Desktop.
Optionals and Dictionary
This file contains hidden or 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
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