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
⚠️ 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
}