Question:

What output will be produced by the code below?

let names = ["Pilot": "Wash", "Doctor": "Simon"]
let doctor = names["doctor"] ?? "Bones"
print(doctor)

Correct answer: "Bones".

Explanation: The code accesses the "doctor" element in the names dictionary, which is not set: dictionaries are case-sensitive. This will cause nil to be returned, which then triggers the nil coalescing operator to set doctor to be "Bones".


Keywords:

© 2017 QuizBucket.org