Reputation: 4113
My setter method won't set a right property. I'm trying to assign the Fahrenheit property to Celsius when you assign it.
var fahrenheit: Float {
get {
return (celsius * 1.8) + 32.0
}
set {
fahrenheit = (fahrenheit - 32) / 1.8
}
}
Why will not my setter method work?
Upvotes: 0
Views: 59
Reputation: 13347
Since you're using set {
, the variable name you should be using to access the value to be set is newValue
.
fahrenheit = (fahrenheit - 32) / 1.8
should become
celsius = (newValue - 32) / 1.8
From the Swift documentation on Computed Properties, for the setter you can define the name of the new value by doing set(newFahrenheit) {
, or you can use the 'Shorthand Setter Declaration' which is just set {
as you're currently doing which defines the variable name to be newValue
.
Upvotes: 3
Reputation: 41226
Because you're assigning to fahrenheit, not centigrade.
fahrenheit = ...
Should be:
celsius = ...
And presumably you have a celsius instance variable already.
Upvotes: 0