📖 Scenario: Imagine you are creating a simple app that tracks distances in kilometers. You want to make your code easier to read by giving a custom name to the Double type that represents distances.
🎯 Goal: You will create a typealias called Distance for Double, then use it to declare variables and print their values.
📋 What You'll Learn
Create a typealias named Distance for the Double type
Declare a variable called homeToSchool of type Distance with the value 3.5
Declare a variable called schoolToPark of type Distance with the value 1.2
Calculate the total distance by adding homeToSchool and schoolToPark
Print the total distance using print
💡 Why This Matters
🌍 Real World
Using <code>typealias</code> helps make code easier to read and understand by giving meaningful names to types, especially in apps dealing with measurements or complex data.
💼 Career
Many software jobs require writing clear and maintainable code. Knowing how to use <code>typealias</code> is a simple way to improve code clarity and teamwork.
Progress0 / 4 steps
1
Create a typealias for Distance
Write typealias Distance = Double to create a new name Distance for the type Double.
Swift
Hint
Use the typealias keyword followed by the new name, an equals sign, and the existing type.
2
Declare distance variables using Distance
Declare a variable called homeToSchool of type Distance and set it to 3.5. Then declare a variable called schoolToPark of type Distance and set it to 1.2.
Swift
Hint
Use var keyword, variable name, colon, type Distance, equals sign, and the value.
3
Calculate total distance
Create a variable called totalDistance of type Distance and set it to the sum of homeToSchool and schoolToPark.
Swift
Hint
Add the two variables and assign the result to totalDistance with type Distance.
4
Print the total distance
Write print(totalDistance) to display the total distance on the screen.
Swift
Hint
Use the print function with totalDistance inside the parentheses.
Practice
(1/5)
1. What is the main purpose of using typealias in Swift?
easy
A. To create a new variable
B. To define a new class or struct
C. To declare a constant value
D. To create a new name for an existing type to improve code readability
Solution
Step 1: Understand the role of typealias
typealias is used to give an existing type a new name, making code easier to read and maintain.
Step 2: Compare with other options
Defining classes, constants, or variables are done with other keywords like class, let, or var, not typealias.
Final Answer:
To create a new name for an existing type to improve code readability -> Option D
Quick Check:
Typealias = new name for type [OK]
Hint: Remember: typealias renames types, not values or classes [OK]
Common Mistakes:
Confusing typealias with variable declaration
Thinking typealias creates new types instead of new names
Using typealias to declare constants or functions
2. Which of the following is the correct syntax to create a typealias for Int named Age in Swift?
easy
A. typealias Age = Int
B. alias type Age = Int
C. type Agealias = Int
D. let Age: Int
Solution
Step 1: Recall the correct syntax for typealias
The correct syntax is typealias NewName = ExistingType.
Step 2: Check each option
typealias Age = Int matches the correct syntax exactly. Options B and C have wrong keyword order or spelling. let Age: Int declares a constant, not a typealias.
Final Answer:
typealias Age = Int -> Option A
Quick Check:
Syntax: typealias Name = Type [OK]
Hint: Syntax is always: typealias NewName = ExistingType [OK]
Common Mistakes:
Swapping keywords like 'alias type' instead of 'typealias'
Using 'let' or 'var' instead of 'typealias'
Misspelling 'typealias'
3. What will be the output of this Swift code?
typealias Text = String
let greeting: Text = "Hello"
print(greeting)
medium
A. Text
B. Hello
C. String
D. Compilation error
Solution
Step 1: Understand the typealias effect
Text is just another name for String. So greeting is a String with value "Hello".
Step 2: Analyze the print statement
Printing greeting outputs the string value "Hello".
Final Answer:
Hello -> Option B
Quick Check:
typealias does not change value, prints "Hello" [OK]
Hint: typealias is just a name, output is the actual value [OK]
Common Mistakes:
Thinking typealias changes the value printed
Expecting the alias name to print instead of the value
Assuming a syntax error due to typealias
4. Identify the error in this Swift code:
typealias Number = Int
var value: Number = "10"
medium
A. Cannot assign a String to a variable of typealias Int
B. typealias syntax is incorrect
C. Variable declaration missing 'let'
D. No error, code runs fine
Solution
Step 1: Understand typealias binding
Number is an alias for Int, so value must be an integer.
Step 2: Check assigned value type
Assigning a string "10" to an integer type variable causes a type mismatch error.
Final Answer:
Cannot assign a String to a variable of typealias Int -> Option A
Quick Check:
Type mismatch error = Cannot assign a String to a variable of typealias Int [OK]
Hint: Alias type must match assigned value type exactly [OK]
Common Mistakes:
Thinking typealias allows different types to be assigned
Ignoring type mismatch errors
Confusing var and let keywords
5. Given this code, what is the type of operation?
typealias Operation = (Int, Int) -> Int
let operation: Operation = { a, b in a + b }
hard
A. An Int value
B. A tuple of two Ints
C. A function that takes two Ints and returns an Int
D. A syntax error due to incorrect closure
Solution
Step 1: Understand the typealias definition
Operation is defined as a function type that takes two Int parameters and returns an Int.
Step 2: Analyze the assigned closure
The closure matches the Operation type: it takes two Ints and returns their sum as Int.
Final Answer:
A function that takes two Ints and returns an Int -> Option C
Quick Check:
Function typealias = A function that takes two Ints and returns an Int [OK]
Hint: Function typealias defines input and output types clearly [OK]
Common Mistakes:
Confusing function type with tuple type
Thinking closure syntax is incorrect
Assuming the alias creates a new type instead of a function signature
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.