Search code examples
iosxcodeswiftoption-typeoptional-variables

Uwrapping optionals - Swift 1.2 Xcode 6.4


I still got problems with optionals, examples made by Apple are not clear to me. I understood that:

  • 1 - optional is a Type of data, so Int? or String? are optionals, not Int or String
  • 2 - optional can be a variable, not a constant, that as far as is optional, could be nil, or something else
  • 3 - in order to use them, you need to unwrap them
  • 4 - you can use optional chaining in order to perform a check (how to decide if unwrap or chaining?)

    • A) how to unwrap Santa in second if?
    • B) how after unwrapping, I can use the variable in other functions?
    • C) how could face this same example by optional chaining?

Thanks in advance

var santa : String?

println(santa)



if let santaExists = santa {
    println("Santa exists! Santa is:  \(santa)")
}
else {
    println("Santa is missing! Santa is: \(santa) or not set") //Santa is missing! Santa is: nil or not set
}

santa = "I am here!"

if let santaExists = santa {
    println("Santa exists! Santa is:  \(santa)") //Santa exists! Santa is:  Optional("I am here!)
}
else {
    println("Santa is missing! Santa is: \(santa) or not set")
}

EDIT: solutionS

var santa : String?

println(santa)



if let santaExists = santa {
    println("Santa exists! Santa is:  \(santa)")
}
else {
    println("Santa is missing! Santa is: \(santa) or not set") //Santa is missing! Santa is: nil or not set
}

santa = "I am here!"

//first way to unwrap, with a " ! "
if let santaExists = santa {
    println("Santa exists! Santa is:  \(santa!)") //Santa exists! Santa is:  I am here! (santa unwrapped)
}
else {
    println("Santa is missing! Santa is: \(santa) or not set")
}

//second way to unwrap don't use santa, but the new constant
if let santaExists = santa {
    println("Santa exists! Santa is:  \(santaExists)") //Santa exists! Santa is:  I am here! (santa unwrapped by let)
}
else {
    println("Santa is missing! Santa is: \(santa) or not set")
}

Solution

  • Firstly, when creating a variable e.g. Santa, please make the first letter lowercase, i.e santa.

    To use unwrapped santa in the second if statement, add an exclamation mark, this tells the compiler that you know this variable is not nil.

    println("Santa exists! Santa is:  \(Santa!)")
    

    You could also use the code below as you have set the contents of the variable Santa to SantaExist as a constant

    if let SantaExist = Santa {
        println("Santa exists! Santa is:  \(SantaExist)") //Santa exists! Santa is:  Optional("I am here!)
    }
    else {
        println("Santa is missing! Santa is: \(Santa) or not set")
    }
    

    As for optional chaining, this is simply putting a question mark in place of the exclamation mark, so that if it is nil it will "fail gracefully" rather than crash the app.

    https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html#//apple_ref/doc/uid/TP40014097-CH21-ID246