📖 Scenario: Imagine you want to create a simple container that can hold any type of item, like a box that can store toys, books, or clothes. Instead of making a new box for each item type, you can make one box that works for all types using generics.
🎯 Goal: You will build a generic Box struct in Swift that can hold any type of item. You will then create boxes for different item types and print their contents.
📋 What You'll Learn
Create a generic struct called Box with a type parameter Item
Add a property item of type Item to Box
Create a variable toyBox of type Box holding a String value
Create a variable numberBox of type Box holding an Int value
Print the contents of both boxes
💡 Why This Matters
🌍 Real World
Generics let you write flexible and reusable code that works with any data type, like containers, collections, or algorithms.
💼 Career
Understanding generics is important for Swift developers to create clean, efficient, and type-safe code used in apps and frameworks.
Progress0 / 4 steps
1
Create a generic struct called Box
Write a generic struct called Box with a type parameter Item. Inside it, declare a property called item of type Item.
Swift
Hint
Use struct Box to declare a generic struct. Inside, add var item: Item.
2
Create a Box holding a String
Create a variable called toyBox of type Box holding the string "Teddy Bear".
Swift
Hint
Use var toyBox = Box(item: "Teddy Bear") to create the box.
3
Create a Box holding an Int
Create a variable called numberBox of type Box holding the integer 42.
Swift
Hint
Use var numberBox = Box(item: 42) to create the box.
4
Print the contents of both boxes
Write two print statements to display the contents of toyBox and numberBox using their item properties.
Swift
Hint
Use print("Toy box contains: \(toyBox.item)") and similarly for numberBox.
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]