CustomDebugStringConvertible
Provide richer debug output.
CustomDebugStringConvertible is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
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 descriptionQuoting 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: Stringfor diagnostic output - Used by
debugPrint,String(reflecting:), and the debugger printusesdescription;debugPrintusesdebugDescription- Put extra internal detail in debug, clean text in description
Next: reflection with Mirror.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “CustomDebugStringConvertible” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “CustomDebugStringConvertible”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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
- CustomStringConvertible
- CustomDebugStringConvertible
- Reflection with Mirror
- Building Debug-Friendly Types