📖 Scenario: You are building a simple library system that stores collections of books. You want to check if all books in a collection are available for borrowing.
🎯 Goal: Create a generic Collection struct that can hold any type of items. Use conditional conformance to make Collection conform to CustomStringConvertible only when its items conform to CustomStringConvertible. Then, check if all books in the collection are available.
📋 What You'll Learn
Create a struct called Book with properties title (String) and isAvailable (Bool).
Create a generic struct called Collection with a property items which is an array of generic type Item.
Make Collection conform to CustomStringConvertible only when Item conforms to CustomStringConvertible.
Add a computed property description in the conditional conformance that returns a string listing all items.
Create a Book instance array and a Collection of books.
Write code to check if all books in the collection are available.
Print the description of the collection and the availability check result.
💡 Why This Matters
🌍 Real World
Conditional conformance helps you write flexible and reusable code that adapts to the capabilities of the types it works with, like collections of different items in apps.
💼 Career
Understanding conditional conformance is important for Swift developers to write clean, efficient, and type-safe code, especially when working with generics and protocols in real-world projects.
Progress0 / 4 steps
1
Create the Book struct
Create a struct called Book with two properties: title of type String and isAvailable of type Bool.
Swift
Hint
Use struct Book { let title: String; let isAvailable: Bool } to define the book.
2
Create the generic Collection struct
Create a generic struct called Collection with a generic type parameter Item. Add a property items which is an array of Item.
Swift
Hint
Use struct Collection { var items: [Item] } to create the generic struct.
3
Add conditional conformance to CustomStringConvertible
Make Collection conform to CustomStringConvertible only when Item conforms to CustomStringConvertible. Add a computed property description that returns a string listing all items separated by commas.
Swift
Hint
Use extension Collection: CustomStringConvertible where Item: CustomStringConvertible and implement var description: String.
4
Create books, collection, check availability, and print output
Make Book conform to CustomStringConvertible by returning the title. Create an array books with these exact books: Book(title: "Swift Basics", isAvailable: true), Book(title: "Advanced Swift", isAvailable: false), Book(title: "iOS Development", isAvailable: true). Create a Collection called bookCollection with books. Check if all books in bookCollection are available using allSatisfy and store the result in allAvailable. Finally, print the bookCollection.description and the availability result with print("All books available: \(allAvailable)").
Swift
Hint
Make Book conform to CustomStringConvertible by returning title. Use allSatisfy on bookCollection.items to check availability.
Practice
(1/5)
1. What does conditional conformance in Swift allow you to do?
easy
A. Make a generic type conform to a protocol only if its generic parameters conform
B. Force all types to conform to a protocol regardless of their generic parameters
C. Automatically generate protocol methods without implementation
D. Conform a type to multiple protocols at once without conditions
Solution
Step 1: Understand generic types and protocols
Generic types can have parameters that may or may not conform to protocols.
Step 2: Role of conditional conformance
Conditional conformance allows a generic type to conform to a protocol only if its generic parameters also conform to that protocol.
Final Answer:
Make a generic type conform to a protocol only if its generic parameters conform -> Option A
Quick Check:
Conditional conformance = generic type depends on parameter conformance [OK]
Hint: Think: 'Conformance depends on generic parameters' [OK]
Common Mistakes:
Assuming all generic types conform unconditionally
Confusing conditional conformance with protocol inheritance
2. Which of the following is the correct syntax to declare conditional conformance for a generic struct Box to the protocol Equatable when its generic type T is Equatable?
easy
A. struct Box<T>: Equatable where T: Equatable {}
B. struct Box<T> where T: Equatable: Equatable {}
C. struct Box<T>: Equatable if T: Equatable {}
D. struct Box<T>: Equatable where T == Equatable {}
Solution
Step 1: Recall Swift conditional conformance syntax
Use where clause after the protocol conformance list to specify conditions.
Step 2: Match syntax to options
struct Box<T>: Equatable where T: Equatable {} correctly places where T: Equatable after : Equatable. Others misuse syntax keywords or order.
Final Answer:
struct Box<T>: Equatable where T: Equatable {} -> Option A
Quick Check:
Correct syntax uses 'where' after protocol list [OK]
C. Extension syntax is invalid; 'where' clause misplaced
D. Cannot conform to Hashable with only T: Equatable; T must be Hashable
Solution
Step 1: Understand Hashable requirements
To conform to Hashable, all stored properties must be Hashable. Here, T must conform to Hashable, not just Equatable.
Step 2: Check the constraint in the code
The code requires T: Equatable, which is insufficient for Hashable conformance.
Final Answer:
Cannot conform to Hashable with only T: Equatable; T must be Hashable -> Option D
Quick Check:
Hashable requires T: Hashable, not just Equatable [OK]
Hint: Hashable needs Hashable constraint, not just Equatable [OK]
Common Mistakes:
Confusing Equatable with Hashable requirements
Thinking 'hash(into:)' must be static
Misplacing 'where' clause in extension
5. You want to make a generic struct Pair conform to Codable only if both generic types A and B conform to Codable. Which of the following is the correct way to declare this conditional conformance?
hard
A. extension Pair: Codable if A: Codable and B: Codable {}
B. extension Pair: Codable where A & B: Codable {}
C. extension Pair: Codable where A: Codable, B: Codable {}
D. extension Pair: Codable where A == Codable, B == Codable {}
Solution
Step 1: Understand multiple constraints in 'where' clause
Swift allows multiple constraints separated by commas in the where clause.
Step 2: Match correct syntax
extension Pair: Codable where A: Codable, B: Codable {} correctly uses where A: Codable, B: Codable. Others use invalid syntax like '&', 'if', or '==' for constraints.
Final Answer:
extension Pair: Codable where A: Codable, B: Codable {} -> Option C
Quick Check:
Multiple constraints separated by commas in 'where' [OK]
Hint: Use commas to separate multiple 'where' constraints [OK]
Common Mistakes:
Using '&' instead of comma in where clause
Using 'if' instead of 'where'
Using '==' instead of ':' for constraints
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.