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 trialDickson Cheung
2,573 PointsIntermediate Swift 3 Challenge - Property Observers
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.red. 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()
Hey! I seem to be stuck on this challenge and am not sure what I'm doing wrong or how to proceed - if someone could maybe steer me in the right direction or point out something im not seeing that would be a really big help, thanks!
class TemperatureController: UIViewController {
var temperature: Double {
didSet {
if temperature > 80.0 {
view.backgroundColor = UIColor.red
} else if temperature < 40.0 {
view.backgroundColor = UIColor.blue
} else {
view.backgroundColor = UIColor.green
}
}
init(temperature: Double) {
self.temperature = temperature
super.init()
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
}
}
1 Answer
Jeff McDivitt
23,970 PointsIt seem that you are just missing a closing brace your code is fine
class TemperatureController: UIViewController {
var temperature: Double {
didSet {
if temperature > 80 {
view.backgroundColor = .red
} else if temperature < 40 {
view.backgroundColor = .blue
}
else {
view.backgroundColor = .green
}
}
}