Bird
0
0

What is wrong with this Swift code snippet?

medium📝 Debug Q7 of 15
iOS Swift - Swift Language Essentials
What is wrong with this Swift code snippet?
let number: Int? = 10
let result: Int = number
print(result)
AOptional Int cannot hold a value
Bprint statement cannot print Int values
CCannot assign optional Int to non-optional Int without unwrapping
DVariable 'result' should be declared as optional
Step-by-Step Solution
Solution:
  1. Step 1: Identify type mismatch

    number is an optional Int, result is a non-optional Int.
  2. Step 2: Understand unwrapping requirement

    You must unwrap optionals before assigning to non-optional variables.
  3. Final Answer:

    Cannot assign optional Int to non-optional Int without unwrapping -> Cannot assign optional Int to non-optional Int without unwrapping
  4. Quick Check:

    Optionals require unwrapping before assignment [OK]
Quick Trick: Unwrap optionals before assigning to non-optionals [OK]
Common Mistakes:
  • Assuming implicit conversion from optional
  • Thinking print cannot handle Int
  • Declaring result as optional unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes