A class that holds two values of possibly different types.
Swift
class Pair<A, B> {
varfirst: A
var second: B
init(_ first: A, _ second: B) {
self.first = firstself.second = second
}
}
Sample Program
This program shows a generic struct Box that can hold any type. It also has a generic function swapValues that swaps two values of the same type. We create boxes for an integer and a string, then swap two integers.
Swift
struct Box<T> {
var item: T
}
func swapValues<T>(_ a: inout T, _ b: inout T) {
let temp = a
a = b
b = temp
}
var intBox = Box(item: 10)
var stringBox = Box(item: "Hello")
print("Before swap: intBox.item = \(intBox.item), stringBox.item = \(stringBox.item)")
var a = 5var b = 8
swapValues(&a, &b)
print("After swap: a = \(a), b = \(b)")
OutputSuccess
Important Notes
Generics help keep your code clean and reusable.
Swift checks types at compile time to prevent errors when using generics.
Summary
Generics let you write code that works with any type.
Use angle brackets <> to declare generic types.
They make your code safer and easier to reuse.
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]