Pick the fix
A custom init sets nameLabel.text = name, where nameLabel is an outlet. It crashes: unexpectedly found nil. Fix?
Hint 1 of 1
When are outlets connected?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Store name in init and set the label in viewDidLoad
Correct. Outlets exist after the view loads.
Why not the others?
- B Call
loadViewIfNeeded()in init. It works, but loads the view early and defeats lazy loading. - C Make the outlet optional and use
nameLabel?.text. It stops the crash but silently never sets the text. - D Move it to viewDidAppear. It works, but the empty label flashes before the text appears.