Hands-On Lab: Build a Library Manager with Structs, Protocols and Error Handling
What We Will Build
A command-style library: add books, borrow and return them, and report problems with clear errors. All code runs as plain Swift.
Step 1: Models and Errors
Start with simple types.
swift
struct Book: Hashable {
let isbn: String
let title: String
}
enum LibraryError: Error, CustomStringConvertible {
case unknownBook(String)
case alreadyBorrowed(String)
case notBorrowed(String)
var description: String {
switch self {
case .unknownBook(let t): return "Unknown book: \(t)"
case .alreadyBorrowed(let t): return "Already borrowed: \(t)"
case .notBorrowed(let t): return "Not borrowed: \(t)"
}
}
}
print(LibraryError.unknownBook("X"))
Output
Unknown book: X
Step 2: A Protocol and the Library
swift
struct Book: Hashable { let isbn: String; let title: String }
enum LibraryError: Error, CustomStringConvertible {
case unknownBook(String), alreadyBorrowed(String), notBorrowed(String)
var description: String {
switch self {
case .unknownBook(let t): return "Unknown book: \(t)"
case .alreadyBorrowed(let t): return "Already borrowed: \(t)"
case .notBorrowed(let t): return "Not borrowed: \(t)"
}
}
}
protocol Lending {
mutating func borrow(_ isbn: String) throws -> Book
mutating func giveBack(_ isbn: String) throws
}
struct Library: Lending {
private var catalog: [String: Book] = [:]
private var borrowed: Set<String> = []
mutating func add(_ book: Book) { catalog[book.isbn] = book }
mutating func borrow(_ isbn: String) throws -> Book {
guard let book = catalog[isbn] else { throw LibraryError.unknownBook(isbn) }
guard !borrowed.contains(isbn) else { throw LibraryError.alreadyBorrowed(book.title) }
borrowed.insert(isbn)
return book
}
mutating func giveBack(_ isbn: String) throws {
guard let book = catalog[isbn] else { throw LibraryError.unknownBook(isbn) }
guard borrowed.remove(isbn) != nil else { throw LibraryError.notBorrowed(book.title) }
}
var availableTitles: [String] {
catalog.values.filter { !borrowed.contains($0.isbn) }.map(\.title).sorted()
}
}
var lib = Library()
lib.add(Book(isbn: "1", title: "Swift Basics"))
lib.add(Book(isbn: "2", title: "Clean Code"))
do {
let b = try lib.borrow("1")
print("borrowed", b.title)
_ = try lib.borrow("1")
} catch {
print("error:", error)
}
do { try lib.giveBack("2") } catch { print("error:", error) }
do { _ = try lib.borrow("9") } catch { print("error:", error) }
try lib.giveBack("1")
print(lib.availableTitles)
Output
borrowed Swift Basics | error: Already borrowed: Swift Basics | error: Not borrowed: Clean Code | error: Unknown book: 9 | ["Clean Code", "Swift Basics"]
What You Practiced
- Structs and Hashable for models
- Enums with associated values for errors
- A protocol with throwing mutating methods
- Dictionaries and Sets for lookup
privatestate with a safe public API
Challenges
- Add a member name to each loan
- Add a limit of 2 books per member
- Return the list of borrowed books
- Write three tests with Swift Testing or XCTest
Conclusion
You built a working system from core Swift features. Errors make every failure path explicit.
Quick Summary: Structs; protocols; errors; collections; encapsulation.

