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:
Step 1: Understand safe casting in Swift
Using 'as?' safely casts the destination to the expected type, avoiding crashes.
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.
Final Answer:
if let destVC = segue.destination as? DetailViewController { destVC.data = value } -> Option D
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
Master "Navigation" in iOS Swift
9 interactive learning modes - each teaches the same concept differently