Consider this Swift code snippet. What will it print?
let number: Int = 10 let text: String = "20" // What happens if we try to add number + text? // print(number + text) // Uncommenting this line causes an error print(number)
Swift does not allow adding an Int and a String directly.
Swift is strongly typed, so it prevents adding an Int and a String without explicit conversion. The commented line causes a compile error. The print(number) line prints 10.
Why does Swift require you to convert types explicitly instead of allowing implicit conversions?
Think about how clear rules help avoid mistakes in your code.
Swift's strong typing forces explicit conversions to avoid bugs caused by mixing incompatible types silently.
Look at this Swift code. What error will it cause?
var value: Int = 5 value = "10"
Check the variable type and the assigned value type.
Swift does not allow assigning a String to an Int variable. This causes a compile-time type mismatch error.
What will this Swift function print?
func multiply(_ a: Int, _ b: Int) -> Int { return a * b } let result = multiply(3, 4) print(result)
Check the multiplication of 3 and 4.
The function multiply takes two Ints and returns their product. 3 * 4 = 12.
Imagine you have a function that takes a user age as an Int. What problem does strong typing help avoid?
Think about what happens if you try to pass text instead of a number.
Strong typing forces the age parameter to be an Int, so passing a String like "twenty" causes a compile-time error, preventing bugs.