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

Not working

Help! Getter and Setter methods not working.

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

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Aside from what Dan Johnson has pointed out about the getter method; there's also an issue in your setter method, you're supposed to use the predefined keyword newValue instead of fahrenheit.

celsius = (newValue - 32.0) / 1.8

cheers.

Dan Johnson
Dan Johnson
40,533 Points

Remember that computed properties aren't actually stored, so you'll need to work with the celsius property to compute the values in the getter:

return (celsius * 1.8) + 32.0