0
0
Swiftprogramming~10 mins

Typealias for custom naming in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Typealias for custom naming
Define typealias
Use alias in code
Compiler treats alias as original type
Code runs with alias
Output or behavior as expected
You create a new name for an existing type using typealias, then use this new name in your code. The compiler treats it as the original type.
Execution Sample
Swift
typealias Age = Int

var myAge: Age = 30
print("My age is \(myAge)")
This code creates a new name 'Age' for the type Int, then uses it to declare a variable and print its value.
Execution Table
StepActionCode LineVariable/TypeValue/Effect
1Define typealiastypealias Age = IntAgeAlias for Int created
2Declare variable using aliasvar myAge: Age = 30myAge30 (Int value) assigned
3Print variableprint("My age is \(myAge)")OutputMy age is 30
4End--Program ends normally
💡 Program ends after printing the value using the alias type
Variable Tracker
Variable/TypeStartAfter Step 2Final
Age (alias)Not definedAlias for IntAlias for Int
myAgeNot defined3030
Key Moments - 2 Insights
Why can we use 'Age' like a type even though it's not a built-in type?
Because 'typealias Age = Int' tells the compiler to treat 'Age' exactly like 'Int', so you can use it anywhere an Int is expected (see execution_table step 1 and 2).
Does 'Age' create a new type different from Int?
No, 'Age' is just another name for Int. The compiler does not create a new type, it just replaces 'Age' with 'Int' during compilation (see concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'myAge' after step 2?
A30
B"Age"
CUndefined
D0
💡 Hint
Check the 'Variable/Type' and 'Value/Effect' columns in step 2 of the execution_table.
At which step does the program print the output?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for the 'Print variable' action in the execution_table.
If we change 'typealias Age = Int' to 'typealias Age = Double', what changes in the variable 'myAge'?
AmyAge becomes a String type variable
BmyAge remains an Int type variable
CmyAge becomes a Double type variable
DmyAge becomes undefined
💡 Hint
Refer to how typealias defines the underlying type in the concept_flow and variable_tracker.
Concept Snapshot
typealias NewName = ExistingType

- Creates a new name for an existing type.
- Use NewName anywhere you use ExistingType.
- Compiler treats NewName exactly as ExistingType.
- Helps make code clearer and easier to read.
Full Transcript
This example shows how to use typealias in Swift to create a new name for an existing type. First, we define 'typealias Age = Int' which means 'Age' is another name for the integer type. Then, we declare a variable 'myAge' of type 'Age' and assign it the value 30. When we print 'myAge', the output is 'My age is 30'. The compiler treats 'Age' exactly like 'Int', so the program runs as if we used Int directly. This helps make code easier to understand by giving meaningful names to types.