Bird
0
0

Which Swift code snippet correctly casts the destination view controller in prepare(for:sender:) to pass data?

easy📝 Syntax Q3 of 15
iOS Swift - Navigation
Which Swift code snippet correctly casts the destination view controller in prepare(for:sender:) to pass data?
AdestVC = segue.destination as! String
Blet destVC = segue.destination as DetailViewController; destVC.data = value
Csegue.destination.data = value
Dif let destVC = segue.destination as? DetailViewController { destVC.data = value }
Step-by-Step Solution
Solution:
  1. Step 1: Understand safe casting in Swift

    Using 'as?' safely casts the destination to the expected type, avoiding crashes.
  2. Step 2: Identify correct syntax

    if let destVC = segue.destination as? DetailViewController { destVC.data = value } uses 'if let' with 'as?' which is the recommended pattern for optional casting.
  3. Final Answer:

    if let destVC = segue.destination as? DetailViewController { destVC.data = value } -> Option D
  4. Quick Check:

    Safe casting with 'as?' and 'if let' [OK]
Quick Trick: Use 'if let' with 'as?' for safe casting [OK]
Common Mistakes:
  • Using forced cast 'as!' without safety
  • Trying to cast to wrong type like String
  • Assigning data without casting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes