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

KOJI NAKAGAWA
KOJI NAKAGAWA
11,261 Points

Do we need "override func viewDidLoad() {}" ?

Hi all. I deleted the below code line from my answer because I thought it was just a sample code. And it was OK for passing the challenge.

But for the real situation, do we still need this func viewDidLoad() method?

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

I'm not sure if the above code is really needed because we don't have to set white color background for this topic. Could you give me some advice when do we use the func viewDidLoad(){}?

Thank you!!

observer.swift
class TemperatureController: UIViewController {

    var temperature: Double {
        didSet{
            if temperature > 80.0 {view.backgroundColor = UIColor.redColor()
            }else if temperature < 40.0 {view.backgroundColor = UIColor.blueColor()
            }else{
                view.backgroundColor = UIColor.greenColor()
            }
        }
    }


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


}

2 Answers

Moritz Lang
Moritz Lang
25,909 Points

Hi Koji, you do not have to call viewDidLoad, if you don't need to set anything. You can override it if you want the code inside it to be excecuted as soon as the view loaded. However your program will run fine without it. If you're using Storyboards, the default background color is white. If you want to change it you can either do that in your Storyboard file or in code using view.backgroundColor = UIColor.greenColor(). If you want the background to be changed before the view appears you can set it in viewDidLoad().

KOJI NAKAGAWA
KOJI NAKAGAWA
11,261 Points

Thank you Moritz! Now I understood!!

Moritz Lang
Moritz Lang
25,909 Points

Awesome, glad that I could help. :)

Moritz Lang
Moritz Lang
25,909 Points

Hi Koji, I don't quite get your question. You can use viewDidLoad() for setting properties or calling functions when the view has been loaded. Because of that the author of the challenge may put this code into it. Please let me know if there is actually something I could explain to you :)

KOJI NAKAGAWA
KOJI NAKAGAWA
11,261 Points

Hi Moritz, thanks for your comment!

I'm just confused because I passed the challenge without the viewDidLoad() so I wondered even if I do not use that function I can still run the program. On the playground, I did not encounter any compile error

I assumed if we use the viewDidLoad(), you will get default background color as white. but if I don't need any background color preset at the beginning, is it fine to write nothing instead set some background color?

for example, ViewController.swift at the beginning has no background color settings, but still it works.

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

Maybe I am not still get how the viewDidLoad works... Thank you for your help.