Bird
0
0

What is the cause of the compilation error in this Swift code?

medium📝 Debug Q7 of 15
Swift - Basics and Runtime
What is the cause of the compilation error in this Swift code?
let number: Int = "123"
AVariable name 'number' is reserved
BMissing semicolon at end of line
CType mismatch: cannot assign String to Int
DInt type requires initialization with zero
Step-by-Step Solution
Solution:
  1. Step 1: Analyze type assignment

    Variable declared as Int cannot be assigned a String value directly.
  2. Step 2: Confirm type safety in Swift

    Swift enforces strict type checking, causing error here.
  3. Final Answer:

    Type mismatch: cannot assign String to Int -> Option C
  4. Quick Check:

    Type mismatch error = A [OK]
Quick Trick: Types must match exactly in Swift assignments [OK]
Common Mistakes:
  • Ignoring type mismatch errors
  • Thinking semicolons are mandatory
  • Assuming variable names are restricted

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes