Bird
Raised Fist0
Swiftprogramming~10 mins

Explicit type annotation in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Explicit type annotation
Declare variable
↓
Add explicit type annotation
↓
Assign value matching type
↓
Use variable in code
↓
Compile checks type correctness
↓
Run program with typed variable
This flow shows how you declare a variable with a specific type, assign a matching value, and then use it safely in your Swift code.
Execution Sample
Swift
var age: Int = 30
print(age)
Declare an integer variable 'age' with explicit type annotation and print its value.
Execution Table
StepActionVariableType AnnotationValueOutput
1Declare variable with typeageIntuninitialized
2Assign value 30ageInt30
3Print variableageInt3030
4End of program
💡 Program ends after printing the value of 'age'.
Variable Tracker
VariableStartAfter AssignmentFinal
ageuninitialized3030
Key Moments - 2 Insights
Why do we write ': Int' after the variable name?
The ': Int' tells Swift that 'age' must always hold an integer. This is shown in the execution_table step 1 where the type is set explicitly.
What happens if we assign a value of a different type?
Swift will give a compile error because the value does not match the declared type. This is checked before step 3 in the execution flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of 'age'?
Auninitialized
B30
C"30" as a string
Dnil
💡 Hint
Check the 'Value' column at step 2 in the execution_table.
At which step is the variable 'age' first given a value?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column for when assignment happens in the execution_table.
If we remove ': Int' from the declaration, what changes in the execution?
AThe program will not compile
BThe variable becomes a string
CSwift infers the type automatically
DThe variable cannot be used
💡 Hint
Swift can infer types if explicit annotation is missing, so check the concept_flow about type annotation necessity.
Concept Snapshot
Explicit type annotation in Swift:
var variableName: Type = value
- Declares variable with fixed type
- Helps catch type errors early
- Makes code clearer and safer
- Example: var age: Int = 30
Full Transcript
This visual execution shows how explicit type annotation works in Swift. First, a variable 'age' is declared with type Int. Then, the value 30 is assigned to it. The program prints the value 30. The type annotation ': Int' ensures the variable only holds integers, preventing errors. If a different type is assigned, Swift will stop compilation. This helps keep code safe and clear.

Practice

(1/5)
1. What does explicit type annotation in Swift do?
easy
A. It removes the need to declare variables.
B. It automatically converts types during runtime.
C. It tells the exact data type of a variable or constant.
D. It makes the program run faster by skipping type checks.

Solution

  1. Step 1: Understand the purpose of explicit type annotation

    Explicit type annotation is used to specify the exact data type of a variable or constant in Swift.
  2. Step 2: Compare with other options

    The other options describe behaviors unrelated to explicit type annotation, such as automatic conversion, removing the need to declare variables, or performance improvements.
  3. Final Answer:

    It tells the exact data type of a variable or constant. -> Option C
  4. Quick Check:

    Explicit type annotation = exact data type [OK]
Hint: Explicit type annotation means writing the type after the variable name [OK]
Common Mistakes:
  • Thinking it changes runtime behavior
  • Confusing with type inference
  • Believing it speeds up code execution
2. Which of the following is the correct syntax for explicit type annotation in Swift?
easy
A. var age: Int = 25
B. var age = Int: 25
C. var age Int = 25
D. var age = 25: Int

Solution

  1. Step 1: Recall Swift syntax for explicit type annotation

    The correct syntax is to write the variable name, then a colon, then the type, then an equals sign and the value.
  2. Step 2: Check each option

    var age: Int = 25 matches this syntax exactly. The other options have misplaced colons or missing punctuation.
  3. Final Answer:

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

    Variable name : Type = value [OK]
Hint: Remember: variableName: Type = value [OK]
Common Mistakes:
  • Putting colon after the value
  • Missing colon between name and type
  • Using equal sign before type
3. What will be the output of this Swift code?
let pi: Double = 3
print(pi)
medium
A. Error: Cannot assign Int to Double
B. 3
C. 3.00
D. 3.0

Solution

  1. Step 1: Analyze the type annotation and assigned value

    The variable pi is declared as a Double but assigned the integer 3. Swift converts the integer 3 to 3.0 automatically.
  2. Step 2: Understand print output for Double

    Printing a Double with value 3 prints as 3.0 by default.
  3. Final Answer:

    3.0 -> Option D
  4. Quick Check:

    Double 3 prints as 3.0 [OK]
Hint: Assigning Int to Double converts to decimal automatically [OK]
Common Mistakes:
  • Expecting a compile error
  • Thinking print shows integer 3
  • Confusing Double formatting
4. Identify the error in this Swift code:
var name: String = 123
print(name)
medium
A. Type mismatch: cannot assign Int to String
B. Variable name is a reserved keyword
C. Missing explicit type annotation
D. No error, prints 123

Solution

  1. Step 1: Check the declared type and assigned value

    The variable name is declared as String but assigned an integer 123, which is a type mismatch.
  2. Step 2: Understand Swift's type safety

    Swift does not allow assigning an Int to a String variable, causing a compile-time error.
  3. Final Answer:

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

    Type mismatch error = Type mismatch: cannot assign Int to String [OK]
Hint: Check if assigned value matches declared type exactly [OK]
Common Mistakes:
  • Assuming implicit conversion from Int to String
  • Ignoring type mismatch errors
  • Thinking variable names are reserved keywords
5. You want to store a list of ages as integers but accidentally write:
var ages: [String] = [20, 25, 30]
What is the best fix using explicit type annotation?
hard
A. Change to var ages = ["20", "25", "30"]
B. Change to var ages: [Int] = [20, 25, 30]
C. Remove type annotation and write var ages = [20, 25, 30]
D. Change to var ages: Int = [20, 25, 30]

Solution

  1. Step 1: Identify the mismatch between type and values

    The variable ages is declared as an array of Strings but assigned integer values, causing a type mismatch.
  2. Step 2: Fix by matching type annotation to actual data

    Changing the type annotation to [Int] matches the integer values in the array.
  3. Final Answer:

    var ages: [Int] = [20, 25, 30] -> Option B
  4. Quick Check:

    Array type matches element types [OK]
Hint: Match array type annotation to the element types [OK]
Common Mistakes:
  • Changing values to strings unnecessarily
  • Removing type annotation without understanding
  • Using single Int type for array
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