Question:

What output will be produced by the code below?

struct Spaceship {
    fileprivate(set) var name = "Serenity"
}

var serenity = Spaceship()
serenity.name = "Jayne's Chariot"
print(serenity.name)

Correct answer: "Jayne's Chariot".

Explanation: Creating a property using fileprivate(set) means that Swift won't let code from other files modify that property directly. However, Swift playgrounds are compiled into a single file when run, and code that exists in the same file as a fileprivate property can access it directly.

 


Keywords:

© 2017 QuizBucket.org