Swift Academy · Lesson

CustomStringConvertible

Define a readable description for your types.

Lesson 1 of 413 steps

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

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.

Free to start

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 “CustomStringConvertible” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “CustomStringConvertible”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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