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

"Add a getter method to the access the value of the fahrenheit property." How do you access the value of variable in get

This makes no sense, how can we be getting a value in get when it is only used to return a value, not to grab a value, and they;re asking you to grab fahrenheit when it's a stored property, it doesn't make sense or wasn't clear.. Thanks!

Temperature.swift
class Temperature {
    var celsius: Float = 0.0
    var fahrenheit: Float {
        get {
          fahrenheit = newValue
        } set {
           celsius = (fahrenheit - 32) / 1.8
        }

    }    
}

1 Answer

Ali Bouland
Ali Bouland
3,479 Points

Hope this helps:

class Temperature {
    var celsius: Float = 0.0
    var fahrenheit: Float {
        get {
            return (celsius * 1.8) + 32.0 /* here you will calculate and **get** 
the value of fahrenheit variable from any given celsius temperature (hence you "get" it)*/
        }
        set {
            celsius = ((newValue-32)/1.8) /* here you will **set**
 the value of the celsius temperature from the calculated 
fahrenheit value (since fahrenheit is a calculated variable, 
it will always be calculated relative to the celsius temperature variable)*/
        }
    }
}