I suppose to be explicit, you should write it as: let j: Int = Int(s ? "0") ? 0Īfter thinking about it some more, it isn't strange it works for the same reason the s ? "0" part of our simple example works (where s is optional but "0" is not). Which is kind of strange, because Int(s ? "0") returns an optional, but 0 is not optional, and Swift creates j as non-optional and doesn't complain. The following works ONLY if the optional string really contains an integer: let j = Int(s ? "0")!ĮDIT: The following works no matter what is in the optional string: let j = Int(s ? "0") ? 0 That second line won't compile "Binary operator '?' cannot be applied to operands of type 'String?' and 'Int'.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |