Question:

What output will be produced by the code below?

struct Spaceship {
    var name: String {
        willSet {
            print("I'm called \(newValue)!")
        }
    }
}

var serenity = Spaceship(name: "Serenity")
serenity.name = "TARDIS"

Answer:

Correct answer: "I'm called TARDIS!".

Explanation: The willSet property observer is triggered only when the initial value is changed, and not when the struct is created using memberwise initialization.


Keywords:

© 2017 QuizBucket.org