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

Gabriel Jones
Gabriel Jones
4,822 Points

Challenge Code

How can I get this to work?

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

Hey Gabriel,

Your solution looks great, however it's looking for the "newValue" keyword in the setter, instead of "fahrenheit". That "newValue" will represent the user's input. Your solution should be modified to look like this:

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

Please mark this as "best answer" if it helps, so future users can reference your question.

Good luck! Kyle

where are the "get" and "set" coming from? Amit mentioned nothing about it in his video regarding Computed Properties

Great question. I actually just looked at the source code posted for that page and it was in there, I haven't actually watched that portion of the series. :)

There's a great bit of reading that may fill in the gaps: Apple Docs

Let me know if there's anything else I can do to help.

Kyle

Thank you Kyle,

I actually managed to get around this challenge without it the "get" and "set". I suppose I will learn about that in the next portion of this course...