Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Intermediate Swift 2 Properties Property Observers

What are the correct background colors

I've been working on the Property observer code and I can't seem to pass this task. I'm unable to test in the playgrounds as I'm pretty sure I've updated to swift 3 so compiler errors are called and the treehouse task causes no compiler errors

to summarise, help!?

Thanks!

Here's the question:

In the editor, I've created a UIViewController subclass, TemperatureController that contains a single property - temperature. Your task is to add a didSet property observer to temperature and set the view's background color depending on value of the temperature. For example, if the temperature is greater than 80, set the view's background color to UIColor.redColor(). Similarly if the temperature is less than 40, set it to blue color, otherwise set it to green color. Note: For changing the view's background color, there's example code in viewDidLoad()

observer.swift
class TemperatureController: UIViewController {
    var temperature: Double {
        didSet {
            if (self.temperature > 80) {
                view.backgroundColor = UIColor.redColor()
            } else if (self.temperature < 40 ){
                view.backgroundColor = UIColor.greenColor()
            } else {
                view.backgroundColor = UIColor.whiteColor()
            }
        }
    }

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

    override func viewDidLoad() {
        view.backgroundColor = UIColor.whiteColor()
    }
}

hey bro your super close the answer is simple re-read the challenge again but just in case you are still stuck the answer is to change the white color to greenColor and will pass

1 Answer

hey bro your super close the answer is simple re-read the challenge again but just in case you are still stuck the answer is to change the white color to greenColor and will pass