When I use print()
on a dictionary in Swift, it comes out nice and pretty in the console, with a key and a value.
object = Optional({
customerId = 1
expression debugPrint(object)
just put the line above in your debugger and hit enter. It will print out contents of our object in more human readable format.
also you can use another one command - po print(data)
, which is easier to remember.
⚠️ The (previously) accepted answer only provided the dictionary as a non-formatted single line string like so:
Optional(["transactionId": 333, "customerId": 111, "extraId": 444])
➡️ As soon as you get more keys and embedded objects/dictionaries it becomes difficult to read.
pjson
) by running this command in your lldb terminal (source):command regex pjson 's/(.+)/expr print(NSString(string: String(data: try! JSONSerialization.data(withJSONObject: %1, options: .prettyPrinted), encoding: .utf8)!))/'
~/.lldbinit
:echo "command regex pjson 's/(.+)/expr print(NSString(string: String(data: try! JSONSerialization.data(withJSONObject: %1, options: .prettyPrinted), encoding: .utf8)!))/'" >> ~/.lldbinit
pjson
alias which you can use in your lldb terminal in XCode:pjson object
let object: Any? = [
"customerId": 111,
"transactionId": 333,
"extraId": 444,
"embeddedDict": [
"foo": true
]
]
❌ Output of po print(data)
Optional(["transactionId": 333, "embeddedDict": ["foo": true], "customerId": 111, "extraId": 444])
✅ Output of pjson
{
"transactionId" : 333,
"embeddedDict" : {
"foo" : true
},
"customerId" : 111,
"extraId" : 444
}