Bird
0
0

SettingsViewController destVC.isDarkMode = true } What will happen if the destination is not SettingsViewController?

medium📝 Predict Output Q5 of 15
iOS Swift - Navigation
Consider this code snippet: override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let destVC = segue.destination as! SettingsViewController destVC.isDarkMode = true } What will happen if the destination is not SettingsViewController?
AApp will crash at runtime due to forced cast
BData will be passed successfully
CThe code will be ignored silently
DCompile-time error occurs
Step-by-Step Solution
Solution:
  1. Step 1: Understand forced casting with 'as!'

    Forced casting assumes the type is correct; if not, it causes a runtime crash.
  2. Step 2: Identify the consequence of wrong type

    If destination is not SettingsViewController, forced cast fails and app crashes.
  3. Final Answer:

    App will crash at runtime due to forced cast -> Option A
  4. Quick Check:

    Forced cast wrong type = runtime crash [OK]
Quick Trick: Avoid forced cast unless sure of type to prevent crashes [OK]
Common Mistakes:
  • Assuming forced cast is safe
  • Expecting compile-time error instead of crash
  • Ignoring type mismatch risks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes