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 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