0
0
Swiftprogramming~30 mins

Why Swift is strongly typed - See It in Action

Choose your learning style9 modes available
Understanding Why Swift is Strongly Typed
📖 Scenario: Imagine you are building a simple app that stores information about fruits and their quantities. You want to make sure the app only accepts the right kind of data for each fruit to avoid mistakes.
🎯 Goal: You will create a small Swift program that shows how Swift uses strong typing to keep data safe and clear. You will declare variables with specific types and see what happens if you try to mix types.
📋 What You'll Learn
Create variables with explicit types
Try to assign wrong types and observe errors
Use type-safe operations
Print the correct values
💡 Why This Matters
🌍 Real World
Strong typing helps developers avoid bugs by making sure data is used correctly, like not mixing numbers and words accidentally.
💼 Career
Understanding strong typing is important for writing safe and reliable Swift code in app development jobs.
Progress0 / 4 steps
1
Create variables with explicit types
Create a variable called appleCount of type Int and set it to 10. Create another variable called fruitName of type String and set it to "Apple".
Swift
Need a hint?

Use var to declare variables and specify the type after a colon.

2
Try assigning a wrong type to a variable
Try to assign the string "Twenty" to the variable appleCount. Write the line appleCount = "Twenty" below the previous code.
Swift
Need a hint?

Swift will show an error because you cannot assign a string to an integer variable.

3
Use type-safe operations
Create a new variable called totalApples of type Int and set it to appleCount + 5. This shows adding numbers safely with strong typing.
Swift
Need a hint?

Use the plus sign + to add numbers of the same type.

4
Print the correct values
Print the value of totalApples using print(totalApples).
Swift
Need a hint?

Use print() to show the value on the screen.