What happens when I use a segue?

Please, help me to understand one line of code.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {      

        if segue.identifier == "toDetailScreenSegue" {
        let detail = segue.destination as! DetailScreenViewController
        detail.name = item.name
        }
    }

What is happening in this line of code below?

let detail = segue.destination as! DetailScreenViewController

  1. Is a copy of the DetailScreenViewController class being made?
  2. And since this is a class, not a structure, then not a separate copy is created, but a reference to the DetailScreenViewController class?

Is this right?

AFAIU, that does not instantiate the VC, nor make a copy of it (would be useless) just get a reference to it. It allows to set some properties in the destination VC. The destination was instantiated before (and stored in segue.destination), when perform segue was called.

The sequence is detailed in UIStoryboardSegue documentation:

Segue objects contain information about the view controllers involved in a transition. When a segue is triggered, but before the visual transition occurs, the storyboard runtime calls the current view controller’s prepare(for:sender:) method so that it can pass any needed data to the view controller that is about to be displayed.

Thanks for your reply!

Do I understand correctly that if it were a structure, not a class, then there would be no such reference?

if it were a structure, not a class

if what were a struct ? A VC is a class in any case.

In addition, if detail were a copy, this

let detail = segue.destination as! DetailScreenViewController
detail.name = item.name

would not change the destination name but just the local's copy, hence missing the very intent.

What happens when I use a segue?
 
 
Q