#7: Value Types and Property Observers
Question
// Swift Quiz
— objc.io (@objcio) April 3, 2019
var screen = CGRect(x: 0, y: 0, width: 320, height: 480) {
didSet { print("Screen changed") }
}
screen.origin.x = 30
var screen2 = screen
screen2.size = CGSize(width: 20, height: 20)
screen2.origin.x = 10
// How many times is didSet called?
Solution
Solution: 1
— objc.io (@objcio) April 4, 2019
The first assignment, screen.origin.x = 30, triggers didSet.
The assignments on copy don't trigger didSet, because CGRect is a value type: assigning screen to screen2 creates an independent copy.https://t.co/s7500CWmAg