Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift Properties Getter and Setter Methods

Nicolo Figiani
Nicolo Figiani
4,012 Points

Can't figure out what is wrong with my code

The code I wrote to solve this challenge looks good to me. Despite this I get:

"Bummer! The setter method to 'fahrenheit' did not assign the right value to che celsius property. (Note: Celsius = (Fahrenheit-32)/1.8) "

as a result.

Am i missing something? Someone can spot what is wrong with my answer?

Thanks..

Temperature.swift
class Temperature {
    var celsius: Float = 0.0
    var fahrenheit: Float {
        get {
          return (celsius * 1.8) + 32.0
        }
        set {
          celsius = (fahrenheit - 32.0) / 1.8
        }
    }    
}

1 Answer

Stone Preston
Stone Preston
42,016 Points

The setter is called whenever fahrenheit is assigned a new value. instead of referencing fahrenheit in the setter, you need to reference the newValue:

class Temperature {
    var celsius: Float = 0.0
    var fahrenheit: Float {
        get {
          return (celsius * 1.8) + 32.0
        }
        set {
          celsius = (newValue - 32.0) / 1.8
        }
    }    
}

alternatively, you can create a custom name for the newValue in the setter like so:

class Temperature {
    var celsius: Float = 0.0
    var fahrenheit: Float {
        get {
          return (celsius * 1.8) + 32.0
        }
        set(newFahrenheit) {
          celsius = (newFahrenheit - 32.0) / 1.8
        }
    }    
}

this makes it a bit more explicit than using the default newValue

Nicolo Figiani
Nicolo Figiani
4,012 Points

You're absolutely right..I'll stick to the first implementation you suggested.

Thanks a lot..

Stone Preston
Stone Preston
42,016 Points

no problem glad I could help