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

What is wrong with the following code?

The challenge fails and says it setting the wrong 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
      }
    }
}

5 Answers

Dave Pinchoff
Dave Pinchoff
4,440 Points

Akshat -

Inside your set code you have this written:

celsius = (fahrenheit - 32) / 1.8

Instead, you should have this written:

celsius = (newValue - 32) / 1.8

because this is a code that instructs the program to change the value of celsius if there is ever a "newValue" given for farenheit. Is that clear? :-)

Ah, I see

Dave Pinchoff
Dave Pinchoff
4,440 Points

Hi there -

In the setter code, you shouldn't be using"fahrenheit" there. You need to use the property "newValue".

Hope this helps!

Hmmn, I don't understand. How?

Oh my Fn God. This got me too. After coming all this way, this was the only challenge that got me. However I don't blame myself. LOL No where in these lesson examples was (newValue) placed inside calculations. I didn't even know you could do that. Bad lesson challenge IMHO. Or they should've had a lesson example where (newValue) was placed inside some sort of calculation. I think this would stump a lot of 'true' noobs.

Dave Pinchoff
Dave Pinchoff
4,440 Points

It is in the lesson example to use newValue - go back and rewatch that video, specifically when he types in the Setter code. Trust me.. it took me a bit, but it's there!!! :-)

I didn't say it's NOT in the video. Read carefully! I said there is not an example of placing "(newValue)" within a calculation/equation.

Want me to prove it??? Here's Amit's code example.

set {
            length = sqrt(newValue)
            width = sqrt(newValue)
        }

Teaching this example without a calculation doesn't make it clear/known to me that it can be used in a calculation since I've never touched code before. I just assumed it was something to be used on its own.

Yes now it's clear because 20/20 ;)