Bird
Raised Fist0
Swiftprogramming~10 mins

Why Swift is strongly typed - Visual Breakdown

Choose your learning style10 modes available
Concept Flow - Why Swift is strongly typed
Declare variable with type
↓
Assign value matching type?
No→Error: Type mismatch
Yes↓
Use variable safely with known type
↓
Compile and run without type errors
Swift requires variables to have a specific type, checks assignments match that type, and prevents errors by stopping code that breaks these rules.
Execution Sample
Swift
var age: Int = 25
age = 30
// age = "thirty" // Error: type mismatch
This code declares an integer variable, assigns valid integer values, and shows an error if a wrong type is assigned.
Execution Table
StepActionVariableValueType CheckResult
1Declare variableageundefinedInt requiredVariable created with type Int
2Assign 25age2525 is IntAssignment successful
3Assign 30age3030 is IntAssignment successful
4Assign "thirty"ageError"thirty" is String, not IntCompile error: type mismatch
💡 Execution stops at step 4 due to type mismatch error preventing invalid assignment.
Variable Tracker
VariableStartAfter 1After 2After 3
ageundefined253030
Key Moments - 2 Insights
Why can't I assign a string to a variable declared as Int?
Because Swift checks the variable's type at assignment (see step 4 in execution_table). Assigning a string to an Int variable causes a compile-time error to prevent bugs.
What happens if I don't specify a type when declaring a variable?
Swift tries to infer the type from the initial value. If no value is given, it requires an explicit type to know what kind of data the variable will hold.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'age' after step 2?
Aundefined
B25
C30
DError
💡 Hint
Check the 'Value' column for step 2 in execution_table.
At which step does the type mismatch error occur?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Look for the 'Result' column mentioning 'Compile error' in execution_table.
If you remove the type declaration ': Int' from 'age', what will Swift do?
AInfer the type from the assigned value
BThrow a compile error immediately
CAllow any type to be assigned later
DSet the variable type to String by default
💡 Hint
Recall the key moment about type inference in Swift.
Concept Snapshot
Swift is strongly typed:
- Variables have fixed types
- Assignments must match variable type
- Type mismatches cause compile errors
- Type inference works if initial value given
- Prevents bugs by catching errors early
Full Transcript
Swift is a strongly typed language. This means every variable has a specific type, like Int or String. When you declare a variable, you tell Swift what type it holds. If you assign a value that doesn't match this type, Swift stops you with an error. For example, if you declare 'age' as an Int, you can only assign numbers to it. Trying to assign a word like "thirty" causes a compile-time error. This helps catch mistakes early and makes your code safer. Swift can also guess the type if you assign a value when declaring the variable. This strong typing helps programmers avoid bugs and write clearer code.

Practice

(1/5)
1. Why does Swift use strong typing for variables and constants?
easy
A. To catch errors early and make code safer
B. To allow variables to change types anytime
C. To make code run faster by ignoring types
D. To let programmers skip declaring types

Solution

  1. Step 1: Understand strong typing purpose

    Strong typing means every variable has a fixed type to avoid mistakes.
  2. Step 2: Identify why Swift uses it

    Swift uses strong typing to catch errors early and keep code safe.
  3. Final Answer:

    To catch errors early and make code safer -> Option A
  4. Quick Check:

    Strong typing = safer code [OK]
Hint: Strong typing means fixed types for safety [OK]
Common Mistakes:
  • Thinking variables can change types freely
  • Believing types are ignored for speed
  • Assuming types are optional in Swift
2. Which of the following is the correct way to declare a strongly typed integer variable in Swift?
easy
A. var age = "25"
B. var age: Int = 25
C. var age: String = 25
D. var age = 25.0

Solution

  1. Step 1: Check variable declaration syntax

    In Swift, you declare a variable with var, specify type with colon, then assign value.
  2. Step 2: Match type and value

    var age: Int = 25 declares age as Int and assigns 25, which is correct for strong typing.
  3. Final Answer:

    var age: Int = 25 -> Option B
  4. Quick Check:

    Correct type and value match = var age: Int = 25 [OK]
Hint: Match variable type and value exactly [OK]
Common Mistakes:
  • Assigning string to an Int type
  • Using wrong type annotation
  • Mixing number types like Int and Double
3. What will be the output of this Swift code?
let number: Int = 10
let text: String = "10"
print(number == Int(text)!)
medium
A. false
B. Compilation error
C. true
D. Runtime error

Solution

  1. Step 1: Understand type conversion

    text is a String "10". Int(text)! converts it to Int 10 safely.
  2. Step 2: Compare values

    number is Int 10, Int(text)! is also 10, so number == Int(text)! is true.
  3. Final Answer:

    true -> Option C
  4. Quick Check:

    10 == 10 is true [OK]
Hint: Convert string to Int before comparing [OK]
Common Mistakes:
  • Assuming string and int compare directly
  • Ignoring forced unwrap with !
  • Expecting false because types differ
4. Identify the error in this Swift code snippet:
var score: Int = "100"
print(score)
medium
A. Variable score is not initialized
B. Missing semicolon at end of line
C. print function syntax is incorrect
D. Type mismatch: assigning String to Int variable

Solution

  1. Step 1: Check variable type and assigned value

    score is declared as Int but assigned a String "100" which is invalid.
  2. Step 2: Understand Swift's strong typing rules

    Swift does not allow assigning a String to an Int variable without conversion.
  3. Final Answer:

    Type mismatch: assigning String to Int variable -> Option D
  4. Quick Check:

    Type mismatch error = Type mismatch: assigning String to Int variable [OK]
Hint: Match variable type and assigned value exactly [OK]
Common Mistakes:
  • Thinking semicolon is required
  • Assuming implicit conversion from String to Int
  • Believing print syntax is wrong
5. You want to create a function in Swift that accepts only integers and returns their square. Why is strong typing important here?
hard
A. It ensures only integers are passed, preventing errors
B. It allows passing any type without restrictions
C. It automatically converts strings to integers
D. It makes the function run faster by ignoring types

Solution

  1. Step 1: Understand function parameter typing

    Strong typing requires the function to accept only Int type, avoiding wrong inputs.
  2. Step 2: Recognize benefits of strong typing in functions

    This prevents runtime errors by catching type mistakes early.
  3. Final Answer:

    It ensures only integers are passed, preventing errors -> Option A
  4. Quick Check:

    Strong typing = safer function inputs [OK]
Hint: Strong typing prevents wrong input types [OK]
Common Mistakes:
  • Thinking any type can be passed
  • Assuming automatic type conversion
  • Believing types slow down functions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard