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 Intermediate Swift Properties Property Observers

Why am I getting this wrong... please explain to me!

I don't just want the answer here please can you help me reach the answer myself. Thank you.

observer.swift
class TemperatureController: UIViewController {
    var temperature: Double {
        willSet {
            print("Old Value: \(temperature)")
        }
        didSet {
            view.backgroundColor = UIColor(temperature)
            print("New Value: \(temperature)")
        }

        init(temperature: Double) {
        self.temperature =
        super.init()
        }

        override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        }
}
}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

HI there, connor hoare ! I received your request for assistance. Let's go through what I see right now are problems.

First, the computed property temperature needs to start with an initial value. It will not compile otherwise. Secondly, check carefully your open and closed curly braces here. You are missing one of the aforementioned before the start of the init. That being said, we only really need the didSet property observer here.

Temperature is a single Double value. Currently, you're trying to set the the backgroundColor to the temperature. Instead, you should have a series of checks here. If the temperature is in a certain range, set it to red etc. You can do this in exactly the same manner as was shown in the last line of the viewDidLoad override method. Right now, you are also trying to print something. Note that printing was not required by the challenge.

In your init method, you should call the super.init first. After this is done the temperature of the object should be set to the temperature that was sent in, but currently, is not being set at all.

Hope this helps! :sparkles: