0
0
Swiftprogramming~10 mins

Why Swift is strongly typed - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why Swift is strongly typed
Declare variable with type
Assign value matching type?
NoError: 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.