Question:

What output will be produced by the code below?

let names: [String?] = ["Barbara", nil, "Janet", nil, "Peter", nil, "George"]

if let firstName = names.first {
    print(firstName)
}

Correct answer: Optional("Barbara").

Explanation: The names array contains values of type String?, but names.first adds an extra level of optionality because it will return nil if there are no items in the array. So, in this code names.first will return String?? (an optional optional String), of which one layer is unwrapped using the if let.

 


Keywords:

© 2017 QuizBucket.org