128
NSDictionary *dictionary = @{@"A" : @"alfa",
                             @"B" : @"bravo",
                             @"C" : @"charlie",
                             @"D" : @"delta",
                             @"E" : @"echo",
                             @"F" : @"foxtrot"};
NSLog(@"%@", dictionary.description);

prints out the following on the console:

{
    A = alfa;
    B = bravo;
    C = charlie;
    D = delta;
    E = echo;
    F = foxtrot;
}

let dictionary: [String : String] = ["A" : "alfa",
                                     "B" : "bravo",
                                     "C" : "charlie",
                                     "D" : "delta",
                                     "E" : "echo",
                                     "F" : "foxtrot"];
print(dictionary)

prints out the following on the console:

["B": "bravo", "A": "alfa", "F": "foxtrot", "C": "charlie", "D": "delta", "E": "echo"]

Is there a way in Swift to get it to pretty print dictionaries where each key-value pair occupies a new line?

5
  • 10
    You could use dump, for example, if the goal is to inspect the dictionary. stackoverflow.com/documentation/swift/3966/logging-in-swift/…
    – Eric Aya
    Commented Aug 4, 2016 at 17:39
  • 21
    print(dictionary as! NSDictionary) cheap trick?
    – BaseZen
    Commented Aug 4, 2016 at 17:41
  • I really the the dump() suggestion since it doesn't require to write any code or cast it. @EricAya, if you post an answer with that remark, I'll mark it as the answer.
    – Toland Hon
    Commented Aug 4, 2016 at 18:35
  • 1
    @TolandHon Done. I've made an answer with an example of the output.
    – Eric Aya
    Commented Aug 4, 2016 at 18:52
  • dump doesn't make it pretty-printed, it gonna looks something like: ▿ 16 key/value pairs #0 ▿ (2 elements) - key: id #1 - super: NSString - super: NSObject - value: 15175100 #2 - super: NSNumber - super: NSValue - super: NSObject ▿ (2 elements) - key: email #3 - super: NSString - super: NSObject - value: america #4 - super: NSMutableString - super: NSString - super: NSObject Commented May 9, 2022 at 11:23

17 Answers 17

160

Casting a dictionary to 'AnyObject' was the simplest solution for me:

let dictionary = ["a":"b",
                  "c":"d",
                  "e":"f"]
print("This is the console output: \(dictionary as AnyObject)")

this is the console output

This is easier to read for me than the dump option, but note it won't give you the total number of key-values.

2
  • It prints cyrillic fonts as Unicode numbers \U0421\U0443\U043f\U0435\U0440 =(
    – Nike Kov
    Commented Mar 12, 2021 at 9:24
  • Tip: make sure to import Foundation. I was playing around with swift on linux and it wont pretty print as shown in the answer without.
    – Chris
    Commented Dec 8, 2022 at 20:28
158

po solution

For those of you want to see Dictionary as JSON without escape sequence in console, here is a simple way to do that:

(lldb) p print(String(data: try! JSONSerialization.data(withJSONObject: object, options: .prettyPrinted), encoding: .utf8)!)
6
  • 1
    Since is an expression and not an object, it should be 'p' and not 'po'. But thanks very much for this solution! Works fine for me Commented Jul 6, 2018 at 10:41
  • @AlessandroFrancucci does it matter? The command seems to do the same thing either way. Commented Nov 9, 2018 at 20:02
  • Now both way of doing it are working. But before doing a "po print" didn't work for me. (po means print object.... which is a bit confusing if you have a print afterwards and not an object imho) Commented Nov 12, 2018 at 10:08
  • 1
    Awesome! just what I needed to print in a nice way userInfo from PushNotification
    – Carmen
    Commented Jun 10, 2020 at 13:19
  • 2
    Check this comment to leverage this in an lldb alias so you don't have to type it everytime!
    – agirault
    Commented Sep 15, 2020 at 22:00
119

You could use dump, for example, if the goal is to inspect the dictionary. dump is part of Swift's standard library.

Usage:

let dictionary: [String : String] = ["A" : "alfa",
                                     "B" : "bravo",
                                     "C" : "charlie",
                                     "D" : "delta",
                                     "E" : "echo",
                                     "F" : "foxtrot"]

dump(dictionary)

Output:

enter image description here


dump prints the contents of an object via reflection (mirroring).

Detailed view of an array:

let names = ["Joe", "Jane", "Jim", "Joyce"]
dump(names)

Prints:

▿ 4 elements
- [0]: Joe
- [1]: Jane
- [2]: Jim
- [3]: Joyce

For a dictionary:

let attributes = ["foo": 10, "bar": 33, "baz": 42]
dump(attributes)

Prints:

▿ 3 key/value pairs
▿ [0]: (2 elements)
- .0: bar
- .1: 33
▿ [1]: (2 elements)
- .0: baz
- .1: 42
▿ [2]: (2 elements)
- .0: foo
- .1: 10

dump is declared as dump(_:name:indent:maxDepth:maxItems:).

The first parameter has no label.

There's other parameters available, like name to set a label for the object being inspected:

dump(attributes, name: "mirroring")

Prints:

▿ mirroring: 3 key/value pairs
▿ [0]: (2 elements)
- .0: bar
- .1: 33
▿ [1]: (2 elements)
- .0: baz
- .1: 42
▿ [2]: (2 elements)
- .0: foo
- .1: 10

You can also choose to print only a certain number of items with maxItems:, to parse the object up to a certain depth with maxDepth:, and to change the indentation of printed objects with indent:.

3
  • 11
    This is not pretty printed JSON, this is just dumping a a variable into the console - not valid JSON. While it does suit OP's needs I believe the question needs rewording to match this. Commented May 16, 2018 at 13:54
  • 7
    @JamesWolfe This is not pretty printed JSON Nobody said it was. The OP asked about pretty printing Swift dictionaries - nobody is talking about JSON, except a few off-topic answerers. The OP's quesiton is not about JSON at all.
    – Eric Aya
    Commented May 16, 2018 at 15:34
  • 1
    @JamesWolfe Also please do not change the question. That would be vandalism. The question is clear as it is, and it's not about JSON. Do not change a question just because some answers talk about something else. Thanks.
    – Eric Aya
    Commented May 16, 2018 at 15:43
41

For debug purpose only, I would convert the Array or Dictionary to a pretty printed json:

public extension Collection {
    
    /// Returns: the pretty printed JSON string or an error string if any error occur.
    var json: String {
        do {
            let jsonData = try JSONSerialization.data(withJSONObject: self, options: [.prettyPrinted])
            return String(data: jsonData, encoding: .utf8) ?? ""
        } catch {
            return "json serialization error: \(error)"
        }
    }
}

Then:

print("\nHTTP request: \(URL)\nParams: \(params.json)\n")

Result on console:

HTTP request: https://example.com/get-data
Params: {
  "lon" : 10.8663676,
  "radius" : 111131.8046875,
  "lat" : 23.8063882,
  "index_start" : 0,
  "uid" : 1
}
5
  • what is bLog here ?
    – Nitesh
    Commented Sep 28, 2017 at 18:51
  • @Nitesh bLog is a simple custom logger with backtrace that I wrote, edited with print().
    – Marco M
    Commented Sep 29, 2017 at 14:34
  • Most beautiful solution. Commented Feb 2, 2020 at 20:22
  • If you want to avoid adding that snippet of code in each of your projects, you can leverage that code with an lldb alias to easily compute the json in the debug terminal (details here).
    – agirault
    Commented Sep 17, 2020 at 13:23
  • What a clever solution and shame for Apple to not added this functionality into the standard library. Commented Mar 14, 2021 at 21:56
40

Just another way using Functional Programming

dictionary.forEach { print("\($0): \($1)") }

Output

B: bravo
A: alfa
F: foxtrot
C: charlie
D: delta
E: echo
5
  • 2
    This should be the top answer. Works perfectly! Commented May 25, 2017 at 20:37
  • Or to be "even more functional"... dictionary.map { "($0): ($1)" }.forEach(print) (tongue-in-cheek comment)
    – Jon Willis
    Commented Nov 14, 2017 at 1:01
  • 3
    This works for OP's [String: String] dictionary, but it's not great for [AnyHashable: Any] dictionaries, where if a value is a dictionary, you're back to Swift's non-pretty printing. Commented Feb 14, 2018 at 23:03
  • I have book marked this answer🙂, because I still cannot remember this syntax 🙄 Commented Jul 23, 2018 at 8:05
  • Similar but store result in a variable: let s = dict.reduce("") { $0 + "\($1.key): \($1.value)\n" }. Suitable only for non-nested dict as @ChristopherPickslay noted. Commented Oct 30, 2023 at 22:44
15

I wouldn't consider a lot of the answers provided here true pretty printed JSON, as when you pass the results into a JSON validator the result is invalid (often due to the code including '=' rather than ':').

The easiest way I've found of doing this is just converting the JSON object to data using the pretty printed writing option then printing a string using the resulting data.

Here is an example:

let jsonData = try! JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)

if let jsonString = String(data: jsonData, encoding: .utf8) {
    print(jsonString)
}

Result:

{
    "jsonData": [
        "Some String"
    ],
    "moreJSONData": "Another String",
    "evenMoreJSONData": {
        "A final String": "awd"
    }
}

EDIT: It's been pointed out that the OP did not ask for JSON, however I find that the answers that recommend just printing or dumping the data into the console provide very little formatting (if any) and are therefore not pretty printing.

I believe that despite the OP not asking for JSON, it is a viable answer as it is a much more readable format for data than the horrendous format that is spat out into the console by xcode/swift.

3
  • 1
    Thanks, With this I was able to pretty print amid debugging via e let jsonData = try! JSONSerialization.data(withJSONObject: response, options: .prettyPrinted);if let jsonString = String(data: jsonData, encoding: .utf8) { print(jsonString) } Commented Nov 13, 2018 at 9:39
  • 1
    This is great! You can leverage this code with an lldb alias to easily compute the json in the debug terminal (details here).
    – agirault
    Commented Sep 16, 2020 at 14:04
  • this is great, thanks!!! Commented Jan 4, 2022 at 1:22
14

Adjusted based on my other answer here.

PrettyPrint JSON solution using LLDB alias

No code needed

  • To get a nice json formatting (indentations, newlines, etc) you can define an lldb alias by running this command in your lldb terminal in XCode (source):
command regex pjson 's/(.+)/expr print(NSString(string: String(data: try! JSONSerialization.data(withJSONObject: %1, options: .prettyPrinted), encoding: .utf8)!))/'
  • The command above only works during the current XCode session. To avoid re-defining the alias everytime you open XCode, run the following command in your macOS terminal. It will append the alias defined above to ~/.lldbinit which LLDB will load everytime XCode starts:
echo "command regex pjson 's/(.+)/expr print(NSString(string: String(data: try! JSONSerialization.data(withJSONObject: %1, options: .prettyPrinted), encoding: .utf8)!))/'" >> ~/.lldbinit
  • This will create the pjson alias which you can use in your lldb terminal in XCode:
pjson object

Comparing the outputs for the following Swift object:

// Using Any? to demo optional & arbitrary Type
let dictionary: Any? = [
    "embedded": [
        "JustForTheSakeOfTheDemo": 42
    ],
    "A" : "alfa",
    "B" : "bravo",
    "C" : "charlie",
    "D" : "delta",
    "E" : "echo",
    "F" : "foxtrot"
]

✅ Output of pjson dictionary

{
  "F" : "foxtrot",
  "D" : "delta",
  "embedded" : {
    "JustForTheSakeOfTheDemo" : 42
  },
  "E" : "echo",
  "A" : "alfa",
  "C" : "charlie",
  "B" : "bravo"
}

❌ Output of p dictionary

(Any?) $R0 = 7 key/value pairs {
  [0] = {
    key = "F"
    value = "foxtrot"
  }
  [1] = {
    key = "D"
    value = "delta"
  }
  [2] = {
    key = "embedded"
    value = 1 key/value pair {
      [0] = (key = "JustForTheSakeOfTheDemo", value = 42)
    }
  }
  [3] = {
    key = "E"
    value = "echo"
  }
  [4] = {
    key = "A"
    value = "alfa"
  }
  [5] = {
    key = "C"
    value = "charlie"
  }
  [6] = {
    key = "B"
    value = "bravo"
  }
}

❌ Output of p (dictionary as! NSDictionary)

(NSDictionary) $R18 = 0x0000000281e89710 {
  ObjectiveC.NSObject = {
    base__SwiftNativeNSDictionaryBase@0 = {
      baseNSDictionary@0 = {
        NSObject = {
          isa = Swift._SwiftDeferredNSDictionary<Swift.String, Any> with unmangled suffix "$"
        }
      }
    }
  }
}

❌ Output of po dictionary

▿ Optional<Any>
  ▿ some : 7 elements
    ▿ 0 : 2 elements
      - key : "F"
      - value : "foxtrot"
    ▿ 1 : 2 elements
      - key : "D"
      - value : "delta"
    ▿ 2 : 2 elements
      - key : "embedded"
      ▿ value : 1 element
        ▿ 0 : 2 elements
          - key : "JustForTheSakeOfTheDemo"
          - value : 42
    ▿ 3 : 2 elements
      - key : "E"
      - value : "echo"
    ▿ 4 : 2 elements
      - key : "A"
      - value : "alfa"
    ▿ 5 : 2 elements
      - key : "C"
      - value : "charlie"
    ▿ 6 : 2 elements
      - key : "B"
      - value : "bravo"

❌ Output of po print(dictionary)

Optional(["F": "foxtrot", "D": "delta", "embedded": ["JustForTheSakeOfTheDemo": 42], "E": "echo", "A": "alfa", "C": "charlie", "B": "bravo"])
2
  • the regex command is in the lldb console (debug console in XCode). If you want to have that command every time you open xcode, you can run the second command in this post in your macOS terminal. I'll update the answer to clarify what terminal should be used for each command.
    – agirault
    Commented Aug 20, 2021 at 21:09
  • Need to escape the ! in the bash command
    – Herno
    Commented Sep 8, 2021 at 1:54
5

You can just use a for loop and print each iteration

for (key,value) in dictionary { 
    print("\(key) = \(value)")
}

Application in extension:

extension Dictionary where Key: CustomDebugStringConvertible, Value:CustomDebugStringConvertible {

    var prettyprint : String {
        for (key,value) in self {
            print("\(key) = \(value)")
        }

        return self.description
    }
}

Alternate application:

extension Dictionary where Key: CustomDebugStringConvertible, Value:CustomDebugStringConvertible {

    func prettyPrint(){
        for (key,value) in self {
            print("\(key) = \(value)")
        }
    }
}

Usage:

dictionary.prettyprint //var prettyprint
dictionary.prettyPrint //func prettyPrint

Output (Tested in Xcode 8 beta 2 Playground):

A = alfa
B = bravo
C = charlie
D = delta
E = echo
F = foxtrot
3
  • 1
    Is there a reason why you made prettyprint a var instead of just a function?
    – Hayden
    Commented Aug 4, 2016 at 18:03
  • Honestly, I don't think it matter (I could be wrong). But if you use it a lot then it's less to type in. But raise an interesting question.
    – Asdrubal
    Commented Aug 4, 2016 at 18:08
  • 3
    Since there's a description and debugDescription already, it might be more appropriate to call the var prettyDescription and return the formatted string.
    – Toland Hon
    Commented Aug 4, 2016 at 18:43
5

The methodology of converting the Swift Dictionary to json and back is the neatest. I use Facebook's chisel which has a pjson command to print a Swift dictionary. Eg:

(lldb) pjson dict as NSDictionary

This should pretty-print the dictionary. This is a much cleaner way to do what has already been suggested. P.S. For now, you'll have to cast dict as NSDictionary because Objective-C runtime doesn't understand Swift dictionaries. I have already raised a PR on chisel to get rid of that restriction.

UPDATE: My PR got accepted. Now you can use psjson command instead of pjson mentioned above.

4

For Swift 3 (& building on the brilliant answer by @Jalakoo), make the following Dictionary extension:

extension Dictionary where Key: ExpressibleByStringLiteral, Value: Any {
    var prettyPrint: String {
        return String(describing: self as AnyObject)
    }
}

then print a dictionary of any hierarchy in a pretty way (better than dump()) using this:

print(dictionary!.prettyPrint)
3

Details

  • Xcode 10.2.1 (10E1001), Swift 5

Solution

extension Dictionary {
    func format(options: JSONSerialization.WritingOptions) -> Any? {
        do {
            let jsonData = try JSONSerialization.data(withJSONObject: self, options: options)
            return try JSONSerialization.jsonObject(with: jsonData, options: [.allowFragments])
        } catch {
            print(error.localizedDescription)
            return nil
        }
    }
}

Usage

let dictionary: [String : Any] = [
                                    "id": 0,
                                    "bool": true,
                                    "int_array": [1,3,5],
                                    "dict_array": [
                                        ["id": 1, "text": "text1"],
                                        ["id": 1, "text": "text2"]
                                    ]
                                 ]
print("Regualr print:\n\(dictionary)\n")
guard let formatedDictionary = dictionary.format(options: [.prettyPrinted, .sortedKeys]) else { return }
print("Pretty printed:\n\(formatedDictionary)\n")

Results

enter image description here

2

swift 5, xcode 10.3:

po print(<your Plist container>)
0

How about:

import Foundation

extension Dictionary {
    var myDesc: String {
        get {
            var v = ""
            for (key, value) in self {
                v += ("\(key) = \(value)\n")
            }
            return v
        }
    }
}


// Then, later, for any dictionary:
print(dictionary.myDesc)
0
extension String {

    var conslePrintString: String {

        guard let data = "\""
            .appending(
                replacingOccurrences(of: "\\u", with: "\\U")
                    .replacingOccurrences(of: "\"", with: "\\\"")
            )
            .appending("\"")
            .data(using: .utf8) else {

            return self
        }

        guard let propertyList = try? PropertyListSerialization.propertyList(from: data,
                                                                             options: [],
                                                                             format: nil) else {
            return self
        }

        guard let string = propertyList as? String else {
            return self
        }

        return string.replacingOccurrences(of: "\\r\\n", with: "\n")
    }
}

let code in extension String and it works fine 

let string = "\(jsonDictionary)".conslePrintString
0

When debugging, output the struct that conform Codable Protocol to the console
use json format.

extension Encodable {
    var jsonData: Data? {
        let encoder = JSONEncoder()
        encoder.outputFormatting = .prettyPrinted
        return try? encoder.encode(self)
    }
}

extension Encodable where Self: CustomDebugStringConvertible {
    var debugDescription: String {
         if let data = self.jsonData,
             let string = String(data: data, encoding: .utf8) {
             return string
         }
         return "can not convert to json string"
     }
}

strcut conform CustomDebugStringConvertible

struct Test: Codable, CustomDebugStringConvertible {
    let a: String
    let b: Int
}

let t = Test(a: "test string", b: 30)

debug print struct

(lldb) p print(t)
{
  "a" : "test string",
  "b" : 30
}
0

Pretty print from Data object:

let jsonObj = try JSONSerialization.jsonObject(with: data, options: [])
            let jsonData = try JSONSerialization.data(withJSONObject: jsonObj, options: [.prettyPrinted])
            print(String(data: jsonData, encoding: .utf8)!)
1
  • 1
    This is great! You can leverage this code with an lldb alias to easily compute the json in the debug terminal (details here).
    – agirault
    Commented Sep 16, 2020 at 14:04
0
 print("{")
    for (key,value) in dictionary {
        if let stringValue = value as? String {
            print("\"\(key)\" = \"\(stringValue.description)\",")
        }else{
            print("\"\(key)\" = \(value),")
        }
    }
    print("}")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.