Zack Zack - 4 months ago 11
iOS Question

Is init?(aDecoder:) or prepareForSegue(_:sender:) called first?

Intro

I am trying to debug my program and I have come across a problem. I am getting the swift error:

Unexpectedly found nil while unwrapping an optional value.
This problem is from an implicitly unwrapped optional. The error is in ViewControllerB's
init?(aDecoder:)
and the place where I set the value is in ViewControllerA's
prepareForSegue(_:sender:)
that uses a "show segue" to go to ViewControllerB.

Problem

This leads me to beleive that the
init?(aDecoder:)
method in ViewControllerB is called before the
prepareForSegue(_:sender:)
in viewControllerA. Am I right?

Jim Jim
Answer

Yes. An object is always initialised before it can be used elsewhere in the application.

If there is work you need to do that depends upon the property being set, it's often appropriate to trigger that work from a didSet observer on the property or in the viewDidLoad() method.

Comments