Is there a way to pretty print Swift dictionaries to the console?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
When debugging Swift applications, you often need to inspect the contents of a dictionary. The default print() output crams everything onto a single line, making it nearly impossible to read for any non-trivial data structure. Swift provides several approaches to produce formatted, human-readable dictionary output, ranging from built-in functions to JSON serialization techniques. Knowing which method to use in each situation will make your debugging workflow significantly faster.
The Problem with Default print()
By default, print() outputs a dictionary as a single-line dump with no indentation:
For small dictionaries this is workable, but once you have nested structures or more than a few keys, the output becomes a wall of text.
Using dump()
Swift's built-in dump() function prints a structured, indented representation of any value. It uses Mirror reflection under the hood, so it works with all Swift types:
The output looks like:
The advantage of dump() is that it requires no setup and handles any type. The downside is that the output format uses reflection markers rather than standard data notation, which can be harder to scan visually.
Using JSONSerialization with .prettyPrinted
For dictionaries that contain only JSON-compatible types (String, Int, Double, Bool, Array, Dictionary, or NSNull), you can serialize them to formatted JSON:
This produces clean, indented JSON output:
The .sortedKeys option ensures consistent ordering, which is helpful when comparing output across runs. This approach only works with JSON-compatible types. If your dictionary contains custom objects or non-bridgeable Swift types, JSONSerialization will throw an error.
Using JSONEncoder with Codable Types
When your data model conforms to Codable, JSONEncoder provides a type-safe alternative:
This approach is preferred for structured model objects because the compiler verifies the data types at compile time, eliminating runtime serialization failures.
Using CustomStringConvertible
For custom types that you print frequently, conforming to CustomStringConvertible lets you define exactly how they appear in print() calls:
Pretty Printing in the Debugger
In Xcode's LLDB debugger, you have additional options. The po (print object) command calls debugDescription or description on an object:
For more structured output, use p with the --raw flag or dump:
You can also set a breakpoint and add a debugger command action to automatically print formatted output without pausing execution.
Common Pitfalls
- Using
JSONSerializationon dictionaries containing non-JSON types (custom objects,Date,URL), which causes silent failure withtry? - Forgetting that
print()on[String: Any]never produces indented output regardless of dictionary size - Assuming dictionary key order is consistent across runs without using
.sortedKeys - Using
dump()in production logging, where its reflection-based format is harder to parse programmatically - Not handling the optional results from JSON serialization, which hides errors
Summary
print()gives single-line output that is only useful for very small dictionariesdump()provides indented, structured output for any Swift type with no setup requiredJSONSerializationwith.prettyPrintedproduces clean JSON output for JSON-compatible dictionariesJSONEncoderis the type-safe choice forCodablemodel objectsCustomStringConvertiblelets you define reusable formatting for custom types- In Xcode, use
poanddump()in the LLDB debugger for quick inspection during debugging sessions
Related reading
- Is there a way to pretty print Swift dictionaries to the console?
- Is there a way to sort a list in python until the first sorted k elements are found?
- Is there a way to store gzip's dictionary from a file?
- Is there a way to use decision trees with categorical variables without one-hot encoding?
- Is there a way to programmatically scroll a scroll view to a specific edit text?
- Is there a way to run Python on Android?
- Is there an edge we can delete without disconnecting the graph?
- Is there an efficient way to cluster a graph according to Jaccard similarity?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.