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

Ye Jin
Ye Jin
3,484 Points

No compilation error in preview

Here is my code in the challenge of getter and setter session, I could not find my mistake, please give me some help.

class Temperature {
    var celsius: Float = 0.0
    var fahrenheit: Float {
        get{
          return fahrenheit
        }
        set{
          celsius = ((newValue - 32) / 1.8)
        }
    }    
    init(c: Float, f: Float){
      self.celsius = c
      self.fahrenheit = f
    }
}
Temperature.swift
class Temperature {
    var celsius: Float = 0.0
    var fahrenheit: Float {
        get{
          return fahrenheit
        }
        set{
          celsius = ((newValue - 32) / 1.8)
        }
    }    
    init(c: Float, f: Float){
      self.celsius = c
      self.fahrenheit = f
    }
}

1 Answer

Richard Lu
Richard Lu
20,185 Points

Hi Ye,

The challenge wants you to add a getter and setter method to the fahrenheit property. For classes with unassigned values you need an initializer, but what you have here is a computed property which does not require you to have one.

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