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

Kapil Soni
Kapil Soni
2,612 Points

Strange that this code is passed whereas on the playground it is throwing an error.

why there is error on super.init() must call a designated initializer of the superclass in UIViewController. also there are solution with if else but why the switch statement is not working as expected?

class TemperatureController: UIViewController {
      var temperature: Double {
         didSet{
             var setColor: UIColor
             switch temperature{
             case 1.0..<40.0: setColor = UIColor.blue
             case 40.0..<60.0: setColor = UIColor.green
             default: setColor = UIColor.red
             }
             view.backgroundColor = setColor
         }
      }
     init(temperature: Double) {
        self.temperature = temperature
        super.init()
     }    
     override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white        
     }    
}

1 Answer

Maria Angelica Dadalt
Maria Angelica Dadalt
6,197 Points

I did an if-else statement instead of switch when I did this challenge and passed. Try that!