A generic function is a function that can work with any type, allowing you to write flexible and reusable code without specifying exact types.
Click to reveal answer
beginner
How do you declare a generic function in Swift?
You declare a generic function by adding a placeholder type inside angle brackets <T> before the function's parameters, like: func example<T>(param: T) { }
Click to reveal answer
beginner
What does the placeholder type <T> represent in a generic function?
The placeholder <T> represents any type that will be specified when the function is called, making the function work with different types without rewriting it.
Click to reveal answer
intermediate
Why use generic functions instead of multiple functions for different types?
Generic functions reduce code duplication by handling multiple types with one function, making code easier to maintain and less error-prone.
Click to reveal answer
intermediate
Example: What does this Swift generic function do?<br><pre>func swapValues<T>(a: inout T, b: inout T) {<br> let temp = a<br> a = b<br> b = temp<br>}</pre>
This function swaps the values of two variables of any type T. It uses inout parameters to change the original variables directly.
Click to reveal answer
What symbol is used to declare a generic type in a Swift function?
A<>
B()
C[]
D{}
✗ Incorrect
Generic types are declared inside angle brackets <> in Swift.
What does the generic placeholder <T> mean in a function?
AAny type specified when calling the function
BA variable name
CA specific type like Int
DA constant value
✗ Incorrect
The placeholder means the function can accept any type specified when called.
Why use generic functions?
ATo avoid using functions
BTo write the same function multiple times
CTo make code slower
DTo handle different types with one function
✗ Incorrect
Generic functions allow one function to work with many types, reducing code duplication.
Which keyword is NOT part of declaring a generic function in Swift?
Afunc
Bvar
C<T>
Dinout
✗ Incorrect
'var' is not required to declare a generic function; 'func' and '' are essential.
What does this generic function do? func identity<T>(value: T) -> T { return value }
ASwaps two values
BChanges the input value
CReturns the input value unchanged
DReturns a fixed value
✗ Incorrect
The function returns the input value exactly as it is.
Explain how to declare and use a generic function in Swift.
Think about how you write a function that can accept any type.
You got /5 concepts.
Why are generic functions useful in programming? Give an example.
Consider how one function can replace many similar functions.
You got /4 concepts.
Practice
(1/5)
1. What is the purpose of declaring a generic function in Swift using <T>?
easy
A. To allow the function to work with any data type without rewriting code
B. To restrict the function to only work with integers
C. To make the function run faster by using a specific type
D. To create a function that only works with strings
Solution
Step 1: Understand generic function purpose
Generic functions use placeholders like <T> to accept any data type.
Step 2: Analyze options
Only To allow the function to work with any data type without rewriting code correctly states that generics allow code reuse for many types without rewriting.
Final Answer:
To allow the function to work with any data type without rewriting code -> Option A
Quick Check:
Generic functions = flexible type use [OK]
Hint: Generics mean one function fits many types [OK]
Common Mistakes:
Thinking generics limit types instead of generalizing
Confusing generics with type-specific functions
Assuming generics improve speed directly
2. Which of the following is the correct syntax to declare a generic function named swapValues in Swift?
easy
A. func swapValues(a: T, b: T) { }
B. func swapValues<T>(a: Int, b: Int) { }
C. func swapValues<T>(a: T, b: T) { }
D. func swapValues(a: T, b: U) { }
Solution
Step 1: Identify correct generic syntax
Generic functions declare type placeholders inside angle brackets after the function name, like <T>.
Step 2: Check parameter types
func swapValues<T>(a: T, b: T) { } correctly uses <T> and parameters of type T. func swapValues(a: T, b: T) { } misses <T>. func swapValues<T>(a: Int, b: Int) { } uses Int which is not generic. func swapValues(a: T, b: U) { } uses two generic types but swap usually uses one.
Final Answer:
func swapValues<T>(a: T, b: T) { } -> Option C
Quick Check:
Generic syntax = func name<T>(...) [OK]
Hint: Generic type <T> goes right after function name [OK]
Common Mistakes:
Forgetting <T> after function name
Using concrete types instead of generic placeholders
Mixing multiple generic types unnecessarily
3. What will be the output of the following Swift code?
The function printValue accepts any type T and prints it.
Step 2: Analyze calls with different types
First call passes an integer 42, second passes a string "Hello". Both print correctly.
Final Answer:
Value is 42
Value is Hello -> Option B
Quick Check:
Generic function prints any type value [OK]
Hint: Generic functions accept any type without error [OK]
Common Mistakes:
Assuming generic functions only accept one type per program
Expecting type mismatch error for different calls
Confusing output order
4. Identify the error in this generic function declaration and fix it:
func compareValues<T>(a: T, b: T) -> Bool {
return a > b
}
medium
A. Remove generic and use Int type instead
B. No error, function is correct
C. Change return type to Int
D. Add constraint: to allow '>' operator
Solution
Step 1: Understand operator usage in generics
The '>' operator requires the type to conform to Comparable protocol.
Step 2: Add constraint to generic type
Adding <T: Comparable> ensures T supports '>' operator.
Final Answer:
Add constraint: <T: Comparable> to allow '>' operator -> Option D
Quick Check:
Generic constraints needed for operators [OK]
Hint: Use when comparing generics [OK]
Common Mistakes:
Ignoring protocol constraints for operators
Changing return type unnecessarily
Removing generics instead of fixing constraints
5. You want to write a generic function findFirstIndex that returns the first index of a value in an array. Which declaration correctly uses generics and constraints to achieve this?
hard
A. func findFirstIndex(array: [T], value: T) -> Int? {
for (index, element) in array.enumerated() {
if element == value {
return index
}
}
return nil
}
B. func findFirstIndex<T>(array: [T], value: T) -> Int? {
for (index, element) in array.enumerated() {
if element == value {
return index
}
}
return nil
}
C. func findFirstIndex(array: [Int], value: Int) -> Int? {
for (index, element) in array.enumerated() {
if element == value {
return index
}
}
return nil
}
D. func findFirstIndex(array: [T], value: T) -> Int? {
for (index, element) in array.enumerated() {
if element == value {
return index
}
}
return nil
}
Solution
Step 1: Understand equality requirement
To compare elements with '==', type T must conform to Equatable.
Step 2: Check generic constraints and function logic
func findFirstIndex(array: [T], value: T) -> Int? {
for (index, element) in array.enumerated() {
if element == value {
return index
}
}
return nil
} correctly declares T: Equatable and uses a loop to find the first matching index.