A generic type declaration lets you write flexible, reusable functions and types that can work with any type, instead of a specific one.
Click to reveal answer
beginner
How do you declare a generic function in Swift?
You add a placeholder type inside angle brackets after the function name, like <T>, and use T as a type inside the function.
Click to reveal answer
intermediate
Example: What does this Swift code do?
func swapValues<T>(a: inout T, b: inout T) { let temp = a; a = b; b = temp }
This function swaps the values of two variables of any type T. It uses generics so it works with Int, String, or any other type.
Click to reveal answer
intermediate
How do you declare a generic type (like a class or struct) in Swift?
You add a placeholder type inside angle brackets after the type name, for example struct Box<T> { var value: T }.
Click to reveal answer
beginner
Why use generic type declarations instead of specific types?
Generics let you write code once and use it with many types, making your code reusable, safer, and easier to maintain.
Click to reveal answer
What symbol is used to declare a generic type placeholder in Swift?
A[]
B{}
C()
D<>
✗ Incorrect
Generic placeholders are declared inside angle brackets <> in Swift.
Which keyword is used to declare a generic function in Swift?
Afunc
Bgeneric func
Cfunc<T>
Dfunc with <T>
✗ Incorrect
You declare a generic function by adding after the function name, like func swapValues(...).
What does the generic type T represent in Swift?
AA placeholder for any type
BA specific type called T
CA variable name
DA function name
✗ Incorrect
T is a placeholder that can represent any type when the function or type is used.
Which of these is a correct generic struct declaration in Swift?
Astruct Box<T> = { var value: T }
Bstruct Box<T> { var value: T }
Cstruct Box<T> (var value: T)
Dstruct Box { var value: T }
✗ Incorrect
The correct syntax uses angle brackets after the struct name and curly braces for the body.
Why are generics useful in Swift?
AThey make code run faster
BThey replace all functions
CThey allow writing reusable and type-safe code
DThey are only for classes
✗ Incorrect
Generics help write reusable code that works with any type safely.
Explain how to declare and use a generic function in Swift.
Think about how you write <T> after the function name and use T as a type.
You got /3 concepts.
Describe the benefits of using generic type declarations in Swift programming.
Why would you want one function or type to work with many types?
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using generic type declaration in Swift?
easy
A. To declare variables with fixed types only
B. To write flexible functions or types that can work with any data type
C. To create functions that only accept integers
D. To make code run faster by avoiding type checks
Solution
Step 1: Understand generics concept
Generics allow writing code that works with any type, not just one fixed type.
Step 2: Compare options with concept
Only To write flexible functions or types that can work with any data type describes this flexibility. Others limit types or mention speed, which is incorrect.
Final Answer:
To write flexible functions or types that can work with any data type -> Option B
Quick Check:
Generics = Flexible code for any type [OK]
Hint: Generics = code that works with any type [OK]
Common Mistakes:
Thinking generics fix types instead of generalizing
Confusing generics with performance optimization
Assuming generics only work with numbers
2. Which of the following is the correct syntax to declare a generic function in Swift?
easy
A. func swapValues(a: T, b: T) { let temp = a; a = b; b = temp }
B. func swapValues<T>(a: T, b: T) { let temp = a; a = b; b = temp }
C. func swapValues<T>(a: T, b: T) -> T { return a }
D. func swapValues<T>(a: inout T, b: inout T) { let temp = a; a = b; b = temp }
Solution
Step 1: Identify correct generic syntax
Generic functions use angle brackets <T> and parameters with inout to swap values.
Step 2: Check each option
func swapValues<T>(a: inout T, b: inout T) { let temp = a; a = b; b = temp } correctly uses func swapValues<T>(a: inout T, b: inout T) and swaps values. Others miss inout or have wrong return types.
Final Answer:
func swapValues<T>(a: inout T, b: inout T) { let temp = a; a = b; b = temp } -> Option D
Quick Check:
Generic function with inout parameters = func swapValues<T>(a: inout T, b: inout T) { let temp = a; a = b; b = temp } [OK]
Hint: Generic functions need <T> and inout for swapping [OK]