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

keith frazier
seal-mask
.a{fill-rule:evenodd;}techdegree
keith frazier
Python Development Techdegree Student 5,771 Points

set method

Im trying to write a set method for a computed property, the preview tab is telling me "UInt8 is not convertible to 'double'", but every number in my code is already a double and the variables are also set to be doubles, so I do not understand what i have to correct.

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

Where is newValue declared? (I ask because I've never done getter / setter, I don't understand where this comes from)

keith frazier
seal-mask
.a{fill-rule:evenodd;}techdegree
keith frazier
Python Development Techdegree Student 5,771 Points

I have newValue declared after the calculation in the curly braces of the setter method so that the fahrenheit can be supplied to the class so the celsius could be indirectly set.

1 Answer

import UIKit

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

The code above should work for this challenge. When using the setter for fahrenheit, the new value has the implicit name newValue. You are using the setter on fahrenheit so it should not be called within the setter.
Hope this helps, Mindy