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

Brandon Overton
Brandon Overton
2,069 Points

Tells me I didn't set the proper value to the 'celsius' property. it's set as celsius = (fahrenheit-32) / 1.8

I am unsure what I need to change for the setter to have it accept the correct value.

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

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Brandon: The concept of the word “newValue” can be a bit difficult to understand. It took me several times watching this video, and doing some research about it until I was able to understand it. I will try to explain it to you, how I see it, and perhaps you will get it too.

First: This is what they are asking in the challenge. You were on the right track, BUT you missed the newValue part.

class Temperature {
    var celsius: Float = 0.0 // initial value of 0.0
    var fahrenheit : Float{
        get{
        return (celsius * 1.8) + 32 // here we are calculating and returning the fahrenheit value
        }
        set {
         celsius = ((newValue - 32) / 1.8)
        }            // ^^ This will be the new value "90" from below

    }

}

var temp = Temperature() // this is the instance of temperature

temp.fahrenheit = 90 // This will be fahrenheits "newValue" **

temp.celsius  // is now 32 celsius
temp.fahrenheit // is now 90 fahrenheit
Brandon Overton
Brandon Overton
2,069 Points

Thanks! The moment you mentioned newValue I understood where it went wrong. Completely makes sense now. Thanks again!!

Jhoan Arango
Jhoan Arango
14,575 Points

Also know that you can give it an explicit name, it does not need to be “newValue”. In order for you to do that, you have to specify it after the word “set”. Like this set(name). Then you can use “name” instead of “newValue”.

        set(randomName) {
         celsius = ((randomName - 32) / 1.8)
        } 

Fantastic explanation.