Predict the output
What does this print?
struct Temperature {
var celsius = 10.0
var fahrenheit: Double {
get { celsius * 9 / 5 + 32 }
set { celsius = (newValue - 32) * 5 / 9 }
}
}
func warm(_ x: inout Double) { x += 18 }
var t = Temperature()
warm(&t.fahrenheit)
print(t.celsius)Hint 1 of 1
What happens when the function returns?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: B 20.0
Correct. 50°F is read, becomes 68°F, and the setter writes back 20°C.
Why not the others?
- A It doesn't compile: computed properties can't be inout. Read-write computed properties can be passed as inout; only get-only ones can't.
- C 10.0. The setter runs on write-back and updates celsius.
- D 28.0. 18 is added to the Fahrenheit value, not to Celsius.