0Pricing
Swift Academy · Lesson

Building Debug-Friendly Types

Combine descriptions and reflection effectively.

Building Debug-Friendly Types is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Combining the Tools

The best types are easy to inspect. Combine CustomStringConvertible, CustomDebugStringConvertible, and Mirror to make values that print clearly and reveal their internals on demand.

A Clean description

Start with user-facing output:

struct Vector: CustomStringConvertible {
    let x: Double
    let y: Double
    var description: String { "(\(x), \(y))" }
}
print(Vector(x: 1, y: 2))  // (1.0, 2.0)

Add Diagnostic debugDescription

Layer on detail for debugging:

struct Vector: CustomStringConvertible, CustomDebugStringConvertible {
    let x: Double
    let y: Double
    var description: String { "(\(x), \(y))" }
    var debugDescription: String { "Vector(x: \(x), y: \(y), len: \(length))" }
    var length: Double { (x * x + y * y).squareRoot() }
}
debugPrint(Vector(x: 3, y: 4))  // Vector(x: 3.0, y: 4.0, len: 5.0)

Auto-Generate description with Mirror

Use reflection to build a description without listing every field by hand:

struct Auto: CustomStringConvertible {
    let a = 1
    let b = "x"
    var description: String {
        let fields = Mirror(reflecting: self).children
            .map { "\($0.label ?? "_")=\($0.value)" }
            .joined(separator: ", ")
        return "Auto(\(fields))"
    }
}
print(Auto())  // Auto(a=1, b=x)

A Reusable Reflection Helper

Factor the Mirror logic into a free function so many types can share one debug formatter.

Shared describe Helper

One helper, reused everywhere:

func reflectiveDescription(_ value: Any) -> String {
    let name = String(describing: type(of: value))
    let parts = Mirror(reflecting: value).children
        .map { "\($0.label ?? "_"): \($0.value)" }
        .joined(separator: ", ")
    return "\(name)(\(parts))"
}
struct P { let id = 7; let tag = "z" }
print(reflectiveDescription(P()))  // P(id: 7, tag: z)

Protocol-Default Implementation

Give a whole family of types reflective debugging via a protocol extension:

protocol Inspectable: CustomStringConvertible {}
extension Inspectable {
    var description: String {
        let parts = Mirror(reflecting: self).children
            .map { "\($0.label ?? "_")=\($0.value)" }
            .joined(separator: ", ")
        return "\(type(of: self))[\(parts)]"
    }
}
struct Widget: Inspectable { let w = 10; let h = 20 }
print(Widget())  // Widget[w=10, h=20]

Pretty-Printing Nested Values

Reflection recurses naturally when children are themselves printable:

struct Inner: CustomStringConvertible { let v = 1; var description: String { "Inner(\(v))" } }
struct Outer: CustomStringConvertible {
    let inner = Inner()
    var description: String { "Outer(\(inner))" }
}
print(Outer())  // Outer(Inner(1))

Using Swift dump

The built-in dump uses Mirror to print a deep, indented tree — handy for quick inspection:

struct Node { let id = 1; let kids = [2, 3] }
dump(Node())
// - id: 1
// - kids: 2 elements ...

Putting It All Together

A polished type: clean description, rich debug info, reflective fallback — easy to log and easy to debug.

struct Order: CustomStringConvertible, CustomDebugStringConvertible {
    let id: Int
    let total: Double
    var description: String { "Order #\(id)" }
    var debugDescription: String { "Order(id: \(id), total: \(total))" }
}
let o = Order(id: 5, total: 9.99)
print(o)       // Order #5
debugPrint(o)  // Order(id: 5, total: 9.99)

Counting Fields Reflectively

Reflection is also handy for quick structural assertions, like counting properties:

struct Profile { let name = "A"; let age = 1; let city = "X" }
let fieldCount = Mirror(reflecting: Profile()).children.count
print("Profile has \(fieldCount) fields")  // Profile has 3 fields

Quick Check

How can you build a description without manually listing every property?

Recap

You learned to build debug-friendly types:

  • Combine description (clean) and debugDescription (detailed)
  • Use Mirror to auto-generate descriptions from children
  • Factor reflection into a helper or protocol-default implementation
  • dump prints a deep reflected tree for quick inspection

Course complete!

Frequently asked questions

Is the “Building Debug-Friendly Types” lesson free?

Yes — the full text of “Building Debug-Friendly Types” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Building Debug-Friendly Types”?

Combine descriptions and reflection effectively. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Building Debug-Friendly Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. CustomStringConvertible
  2. CustomDebugStringConvertible
  3. Reflection with Mirror
  4. Building Debug-Friendly Types
← Back to Swift Academy