Question:

What output will be produced by the code below?

struct Spaceship {
    var name: String

    func setName(_ newName: String) {
        name = newName
    }
}
var enterprise = Spaceship(name: "Enterprise")
enterprise.setName("Enterprise A")
print(enterprise.name)

Correct answer: This code will not compile.

Explanation: The setName() method is attempting to change the name parameter, which is prohibited unless the mutating keyword is used. The correct code should be mutating func setName(_ newName: String).


Keywords:

© 2017 QuizBucket.org