Back to questions
JuniorTheoryCommon
What does the program print?
struct Score {
var points = 0 {
didSet {
print(points - oldValue, terminator: " ")
}
}
}
var s = Score()
s.points = 5
s.points += 35 3
- Expecting
5 8: thinkingoldValueis the property's first value. It is the value just before the current write, so the second number is8 - 5. - Expecting
0 0: thinking the property still holds the old value insidedidSet.didSetruns after the write, sopointsalready holds the new value.
- What does the program print if the observer is
willSetwithprint(newValue - points)? - Why does
var s = Score()print nothing?