A type alias in Swift is a way to give a new name to an existing type. It helps make code easier to read and understand by using meaningful names.
Click to reveal answer
beginner
How do you declare a type alias in Swift?
You use the keyword typealias followed by the new name and the existing type. For example: typealias Age = Int
Click to reveal answer
beginner
Why use type aliases for readability?
Type aliases make code easier to understand by giving descriptive names to types. This helps when the original type is complex or not clear, like function types or tuples.
Click to reveal answer
beginner
Can type aliases change the behavior of a type?
No, type aliases only create a new name for an existing type. They do not create new types or change how the type works.
Click to reveal answer
beginner
Example: What does this code do? typealias CompletionHandler = (Bool) -> Void
It creates a new name CompletionHandler for a function type that takes a Bool and returns nothing (Void). This makes code using this function type easier to read.
Click to reveal answer
What keyword is used to create a type alias in Swift?
Arename
Balias
Ctypedef
Dtypealias
✗ Incorrect
The correct keyword in Swift to create a type alias is typealias.
What does a type alias do?
AGives a new name to an existing type
BCreates a new type with different behavior
CDeletes an existing type
DChanges the data stored in a type
✗ Incorrect
A type alias gives a new name to an existing type without changing its behavior.
Which of these is a valid type alias declaration in Swift?
Atypealias Score = Double
Balias Score = Double
Ctypedef Score = Double
Drename Score = Double
✗ Incorrect
Only typealias Score = Double is valid Swift syntax.
Why might you use a type alias for a function type?
ATo make the function run faster
BTo change the function's return type
CTo make the function type easier to read
DTo delete the function
✗ Incorrect
Type aliases help make complex function types easier to read and understand.
Does a type alias create a new type in Swift?
AYes, it creates a new type
BNo, it only creates a new name for an existing type
CYes, but only for classes
DNo, it deletes the old type
✗ Incorrect
Type aliases only create a new name for an existing type; they do not create new types.
Explain what a type alias is and why it helps with code readability in Swift.
Think about giving a simpler or clearer name to a complex type.
You got /3 concepts.
Write a Swift type alias for a function that takes a String and returns an Int. Explain why you might want to use this alias.
Use (String) -> Int as the function type.
You got /3 concepts.
Practice
(1/5)
1.
What is the main purpose of using typealias in Swift?
easy
A. To create a new name for an existing type to improve code readability
B. To create a completely new data type different from existing ones
C. To declare a variable with a fixed value
D. To define a function that returns a type
Solution
Step 1: Understand what typealias does
It creates a new name for an existing type, not a new type itself.
Step 2: Identify the purpose of this feature
This helps make code easier to read and understand by using meaningful names.
Final Answer:
To create a new name for an existing type to improve code readability -> Option A
Quick Check:
Type alias = New name for existing type [OK]
Hint: Remember: typealias renames, it doesn't create new types [OK]
Common Mistakes:
Thinking typealias creates a new type
Confusing typealias with variable declaration
Assuming typealias defines functions
2.
Which of the following is the correct syntax to create a type alias named Age for the Int type in Swift?
easy
A. type Agealias = Int
B. alias type Age = Int
C. typealias Age = Int
D. Age typealias = Int
Solution
Step 1: Recall the correct syntax for typealias
The syntax is: typealias NewName = ExistingType.
Step 2: Match the syntax with the options
typealias Age = Int matches the correct syntax exactly.
Final Answer:
typealias Age = Int -> Option C
Quick Check:
Correct syntax = typealias Name = Type [OK]
Hint: Syntax always starts with 'typealias' keyword [OK]
A. Using Name inside the print instead of the parameter name
B. Incorrect typealias syntax
C. Missing return type in function
D. Cannot use typealias as function parameter type
Solution
Step 1: Check the function parameter and usage
The function parameter is named name, but inside print, Name (the type alias) is used instead.
Step 2: Understand the difference between parameter and typealias
Name is a type alias, not a variable holding the value. The print should use name to print the argument.
Final Answer:
Using Name inside the print instead of the parameter name -> Option A
Quick Check:
Use parameter name, not typealias name [OK]
Hint: Use variable names, not typealias names inside functions [OK]
Common Mistakes:
Confusing type alias name with variable name
Assuming typealias can be used as variable
Ignoring case sensitivity
5.
You want to use a type alias to simplify a complex dictionary type in Swift. The dictionary maps String keys to arrays of tuples, where each tuple contains an Int and a Bool. Which of the following type alias declarations correctly represents this?
hard
A. typealias DataMap = [(String, [Int, Bool])]
B. typealias DataMap = [String: [Int: Bool]]
C. typealias DataMap = [String: (Int, Bool)]
D. typealias DataMap = [String: [(Int, Bool)]]
Solution
Step 1: Understand the required type structure
The dictionary keys are String. The values are arrays of tuples. Each tuple has an Int and a Bool.
Step 2: Match the options with the structure
typealias DataMap = [String: [(Int, Bool)]] matches exactly: a dictionary from String to array of (Int, Bool) tuples.
Final Answer:
typealias DataMap = [String: [(Int, Bool)]] -> Option D