print() vs dump() in Swift: What’s the Difference?

Discover the key differences between Swift's `print()` and `dump()` functions. Understand their impact on your development.

Francesco Leoni

• 3 min read

Swift offers more than one way to output information to the console, but not all output functions are created equal. Two of the most commonly used are print() and dump(). While both can help you understand what’s happening in your code, they serve distinct purposes and have unique strengths.

print()

The print() function is the go-to tool for displaying simple, readable information in the console. It outputs the textual representation of the value you pass to it, making it ideal for strings, numbers, and basic data types.

  • Outputs a straightforward, human-readable string
  • Works seamlessly with standard library types
  • Default separator is a space; default terminator is a newline
  • For custom objects, only prints the type name unless you conform to CustomStringConvertible
Example:
let name = "Arthur"
let age = 42
 
print("Name:", name, "Age:", age)
 
// Output: Name: Arthur Age: 42

For custom types:

struct Person {
  var name: String
  var age: Int
}
 
let person = Person(name: "Ford", age: 35)
 
print(person)
// Output: Person(name: "Ford", age: 35)

If you don’t implement a custom description, the output may be less informative.

dump()

The dump() function is designed for deeper inspection. It prints a detailed, structured breakdown of an object, including all its properties and their current values. This makes it invaluable for debugging complex data structures and custom types.

  • Reveals internal properties and hierarchy of objects
  • No need to implement extra protocols or custom descriptions
  • Offers parameters for customizing depth, indentation, and output size
  • Output is more verbose and structured, making it ideal for debugging
Example:
dump(person)

Typical output:

// ▿ Person
// - name: "Ford"
// - age: 35

Even if the type doesn’t conform to CustomStringConvertible, dump() will still show its internals.

When to Use print() vs dump()

  • Use print() for:

    * Quick, simple output of strings, numbers, or basic collections

* Logging progress or status messages

* Displaying custom objects with a tailored description

  • Use dump() for:

    * Inspecting the full internal state of structs, classes, or collections

* Debugging nested or complex data structures

* Understanding object hierarchies without extra code

Performance Considerations

  • print() is lightweight and fast, especially for simple types.
  • dump() can be slower, as it introspects objects and may output large amounts of data for complex or deeply nested structures.

Code Comparison

struct Book {
  var title: String
  var author: String
  var pages: Int
}
 
let book = Book(title: "Swift Essentials", author: "Jane Doe", pages: 320)
 
print(book)
// Output: Book(title: "Swift Essentials", author: "Jane Doe", pages: 320)
dump(book)
// Output:
// ▿ Book
// - title: "Swift Essentials"
// - author: "Jane Doe"
// - pages: 320

Key Takeaways

  • print() gives you a quick, readable output—best for simple data and custom descriptions.
  • dump() provides a deep, property-level view—best for debugging and exploring complex objects.
  • Choose the right tool based on your debugging needs and the complexity of your data.

Tip

For everyday logging, stick with print(). When you need to see every detail, reach for dump().

Happy coding!

If you have any question about this article, feel free to email me or tweet me @franceleonidev and share your opinion.

Thank you for reading and see you in the next article!

Share this article

Related articles


FLCharts: Create Bar Chart easily

Create beautiful and highly customisable bar, line, radar charts and many more with FLCharts, a flexible, easy-to-use library.

• 3 min read

ChartsSwift

How to Create Swift Macros with Xcode 15

Discover the new Swift Macro, speed up your workflow and make your code easier to read. Available for Swift 5.9 and Xcode 15.

• 8 min read

Swift

Make Code Easier to Write and Read in Swift 5.9

Improve your code with inline if-else and switch statements available in Swift 5.9. Make your codebase less tedious to write.

• 3 min read

Swift