0Pricing
Swift Academy · Lesson

CustomStringConvertible

Define a readable description for your types.

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

What Is CustomStringConvertible?

This protocol lets you control how your type appears when printed or interpolated into a string. You provide a description property.

The Default Is Ugly

Without it, structs print their full memberwise form, which is often noisy:

struct Point { let x: Int; let y: Int }
print(Point(x: 1, y: 2))  // Point(x: 1, y: 2)

Adding description

Conform and provide var description: String:

struct Point: CustomStringConvertible {
    let x: Int
    let y: Int
    var description: String { "(\(x), \(y))" }
}
print(Point(x: 1, y: 2))  // (1, 2)

Used by print and String(describing:)

Your description is what print and string interpolation use:

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

description vs Direct Properties

description is for human-readable, user-facing text. It is not meant to be parsed back — that is a different concern (Codable). Keep it readable.

Formatting an Enum

Give enums friendly names:

enum Suit: CustomStringConvertible {
    case hearts, spades
    var description: String {
        switch self {
        case .hearts: return "Hearts"
        case .spades: return "Spades"
        }
    }
}
print(Suit.hearts)  // Hearts

Composing Descriptions

Build a description from member descriptions:

struct Card: CustomStringConvertible {
    let rank: Int
    let suit: String
    var description: String { "\(rank) of \(suit)" }
}
let cards = [Card(rank: 1, suit: "H"), Card(rank: 13, suit: "S")]
print(cards.map { $0.description })  // ["1 of H", "13 of S"]

Inside Collections

Printing a collection calls each element description:

struct Tag: CustomStringConvertible {
    let name: String
    var description: String { "#\(name)" }
}
print([Tag(name: "swift"), Tag(name: "ios")])  // [#swift, #ios]

Interpolation Uses description

String interpolation automatically picks up your description:

struct Temperature: CustomStringConvertible {
    let celsius: Int
    var description: String { "\(celsius)C" }
}
let t = Temperature(celsius: 21)
print("Now: \(t)")  // Now: 21C

Keep It Cheap

description may be called often (logging, debugging). Avoid heavy work inside it; compute a simple string from stored properties.

struct ID: CustomStringConvertible {
    let value: Int
    var description: String { "ID#\(value)" }
}
print(ID(value: 42))  // ID#42

Conditional Formatting

description can branch on state to read naturally:

struct Stock: CustomStringConvertible {
    let count: Int
    var description: String {
        count == 0 ? "Out of stock" : "\(count) in stock"
    }
}
print(Stock(count: 0))  // Out of stock
print(Stock(count: 5))  // 5 in stock

Quick Check

Which property does CustomStringConvertible require?

Recap

You learned CustomStringConvertible:

  • Provide var description: String for user-facing text
  • Used by print, String(describing:), and interpolation
  • Keep it readable and cheap; do not aim for round-tripping
  • Works inside collections automatically

Next: CustomDebugStringConvertible.

Frequently asked questions

Is the “CustomStringConvertible” lesson free?

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

Define a readable description for your types. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

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