0Pricing
Swift Academy · Lesson

CustomDebugStringConvertible

Provide richer debug output.

CustomDebugStringConvertible is a free Swift Academy lesson on CoddyKit — lesson 2 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.

A Separate Debug View

CustomDebugStringConvertible provides debugDescription — the representation used by debugPrint, the LLDB po command, and the debugger. It can be more detailed than description.

Adding debugDescription

Conform and supply the property:

struct Point: CustomDebugStringConvertible {
    let x: Int
    let y: Int
    var debugDescription: String { "Point(x: \(x), y: \(y))" }
}
debugPrint(Point(x: 1, y: 2))  // Point(x: 1, y: 2)

debugPrint vs print

print uses description; debugPrint uses debugDescription:

struct Money: CustomStringConvertible, CustomDebugStringConvertible {
    let cents: Int
    var description: String { "$\(Double(cents)/100)" }
    var debugDescription: String { "Money(cents: \(cents))" }
}
let m = Money(cents: 250)
print(m)       // $2.5
debugPrint(m)  // Money(cents: 250)

When to Provide Both

Use description for clean user output and debugDescription for diagnostic detail (raw values, internal state). Providing both gives the best of each context.

More Detail in Debug

Surface internals that users should not see:

struct Session: CustomStringConvertible, CustomDebugStringConvertible {
    let user: String
    let token: String
    var description: String { "Session(\(user))" }
    var debugDescription: String { "Session(user: \(user), token: \(token))" }
}
let s = Session(user: "ann", token: "xyz")
print(s)       // Session(ann)
debugPrint(s)  // Session(user: ann, token: xyz)

Debug Output in Collections

debugPrint of a collection uses each element debugDescription:

struct Tag: CustomDebugStringConvertible {
    let name: String
    var debugDescription: String { "Tag(\(name))" }
}
debugPrint([Tag(name: "a"), Tag(name: "b")])  // [Tag(a), Tag(b)]

String(reflecting:)

Just as String(describing:) uses description, String(reflecting:) uses debugDescription:

struct Code: CustomDebugStringConvertible {
    let n: Int
    var debugDescription: String { "Code<\(n)>" }
}
print(String(reflecting: Code(n: 7)))  // Code<7>

Fallback Behavior

If a type provides only description, debugPrint falls back to it. If it provides only debugDescription, plain print still uses the default mirror form:

struct Only: CustomStringConvertible {
    let v: Int
    var description: String { "Only(\(v))" }
}
debugPrint(Only(v: 3))  // Only(3)  -- falls back to description

Quoting in Debug Output

Note that debugPrint of a String adds quotes, reflecting its debug nature:

print("hi")        // hi
debugPrint("hi")   // "hi"

Diagnostics Without Leaking

Keep secrets out of description but allow them in debugDescription only if logs are safe. Choose deliberately what each one reveals.

struct Key: CustomStringConvertible {
    let secret: String
    var description: String { "Key(***)" }
}
print(Key(secret: "abc"))  // Key(***)

Debug Detail for an Enum

Enums benefit from debugDescription that exposes the raw case and payload:

enum Load: CustomDebugStringConvertible {
    case idle
    case progress(Int)
    var debugDescription: String {
        switch self {
        case .idle: return "Load.idle"
        case .progress(let p): return "Load.progress(\(p))"
        }
    }
}
debugPrint(Load.progress(40))  // Load.progress(40)

Quick Check

Which function uses debugDescription rather than description?

Recap

You learned CustomDebugStringConvertible:

  • Provide var debugDescription: String for diagnostic output
  • Used by debugPrint, String(reflecting:), and the debugger
  • print uses description; debugPrint uses debugDescription
  • Put extra internal detail in debug, clean text in description

Next: reflection with Mirror.

Frequently asked questions

Is the “CustomDebugStringConvertible” lesson free?

Yes — the full text of “CustomDebugStringConvertible” 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 “CustomDebugStringConvertible”?

Provide richer debug output. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “CustomDebugStringConvertible” 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