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

Payton Dwight
Payton Dwight
5,751 Points

Why won't this compile?

Not getting errors on the preview page

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

    init (celsius: Float){
      self.celsius=celsius
    }    
}

var temp=Temperature(celsius: 69.0)
temp.fahrenheit=90.0

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

Your setter needs to take a parameter in parenthesis next to "set".

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

I don't think you need to definite the init method.