I am learning Swift and I have doubts about implicitly deferred options.
I have a function that returns String optionally:
func findApt(aptNumber :String) -> String? { let aptNumbers = ["101", "202", "303", "404"] for element in aptNumbers { if (element == aptNumber) { return aptNumber } }
and an if statement for safe deployment, which uses implicitly Unwrapped Optional as a constant:
if let apt :String! = findApt("404") { apt println(apt) println("Apartment found: \(apt)") }
On the first line, apt unfolds on its own (β404β is displayed on the result panel), but apt does not expand on the other two lines: the result panels and consoles show that the printed values ββare optional (β404β) and Apartment found: Optional (" 404 "), and in order for 404 to appear on the console, I have to use! a symbol, which, if I understand correctly, is necessary only for the manual expansion of the usual options.
Why is this happening?
My guess is that when passing println (), apt is implicitly converted from Implicitly Unwrapped Optional to regular Optional, which leads to the appearance of optional ("404") text. I like some expert advice on this, as I just start with this language.
swift swift-playground optional
MustangXY Aug 22 '15 at 15:14 2015-08-22 15:14
source share