Bird
0
0

Given the code snippet below, what will be the value of destinationVC.userName after the segue?

medium📝 Predict Output Q13 of 15
iOS Swift - Navigation
Given the code snippet below, what will be the value of destinationVC.userName after the segue?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "showDetail" {
    let destinationVC = segue.destination as! DetailViewController
    destinationVC.userName = "Alice"
  }
}
A"Alice"
Bnil
CAn empty string ""
DCauses a runtime crash
Step-by-Step Solution
Solution:
  1. Step 1: Check the segue identifier and casting

    The code checks if the segue identifier is "showDetail" and casts the destination to DetailViewController safely using forced cast.
  2. Step 2: Assign the userName property

    It sets destinationVC.userName to "Alice" before the segue completes.
  3. Final Answer:

    "Alice" -> Option A
  4. Quick Check:

    prepare sets userName = "Alice" [OK]
Quick Trick: Check identifier and cast before assigning data [OK]
Common Mistakes:
  • Forgetting to check segue.identifier
  • Not casting destinationVC correctly
  • Assuming userName remains nil

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes