0
0
Swiftprogramming~30 mins

Conditional conformance in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Conditional Conformance in Swift
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Make Book conform to CustomStringConvertible by returning title. Use allSatisfy on bookCollection.items to check availability.