Last active
December 14, 2015 13:59
-
-
Save kurko/ac90d3134bf183b94eb2 to your computer and use it in GitHub Desktop.
Swift: confusion optionals
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
var myDictionary: [String: String] = [:] // Same behavior with `String!` | |
myDictionary["My key"] = "My non-optional string" // This is a normal string | |
// It would be fine if it was wrapped in an optional just for print() | |
print("My output: \(myDictionary["My key"])") // outputs 'My output: Optional("My non-optional string")")' | |
// However, it's wrapped by default and that has serious implications | |
// in the source code. I spent hours debugging a problem last weekend | |
// just to find out that a mere string had become an Optional. | |
// | |
// In the following, I don't understand the reasoning for it to fail | |
// until I checked that `if` myself and found out that it was an Optional: | |
print(myDictionary["My Key"] == "My non-optional String") // returns false |
carlosgaldino
commented
Dec 14, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment