what is newValue in swift? and why it cant be changed | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what is newValue in swift? and why it cant be changed

why i cant change newValue when its not initialized anywhere class Temperature { var celsius: Float = 0.0 var fahrenheit: Float { get { return ((celsius * 1.8) + 32.0) } set { celsius = (newValue - 32)/1.8 } } } let temp = Temperature() temp.fahrenheit = 99 print (temp.celsius)

5th Dec 2020, 11:38 PM
Dzondzula
Dzondzula - avatar
1 Answer
0
`fahrenheit` is a property member of `Temperature` class, it supports getter and setter. The getter returns the calculation result of `celcius` value to `fahrenheit`. The setter adjusts `celcius` value based on given `newValue` (expected to be a value in fahrenheit) There is no need to change `newValue` because it was an input rather than output. For the example snippet shown in Description, `newValue` stores the property value assigned at second to last line (temp.fahrenheit = 99) (Edited)
6th Dec 2020, 5:15 AM
Ipang