当我在Swift中的字典上使用print()
时,它在控制台中显得漂亮漂亮,带有键和值。
object = Optional({
customerId = 111;
transactionId = 333;
extraId = 444;
})
当我在同一个字典上运行po
时,会吐出这个难以理解的无意义转储。
▿ Optional<Any>
▿ some : 3 elements
▿ 0 : 2 elements
▿ .0 : transactionId
- .1 : 333
▿ 1 : 2 elements
▿ .0 : customerId
- .1 : 111
▿ 2 : 2 elements
▿ .0 : extraId
- .1 : 444
仅使用p
更糟糕
(Any?) $R8 = some {
payload_data_0 = 0x0000000170e679c0 {
Swift._SwiftNativeNSDictionary = {}
_heapBufferBridged_DoNotUse = 0x0000000170e679d0 {}
nativeStorage = {
buffer = 0x00000001703e4300 {
Swift.ManagedBuffer = {}
}
initializedEntries = (values = 0x00000001703e4328, bitCount = 4)
keys = 0x00000001703e4330
values = 0x00000001703e4390
}
}
payload_data_1 = 0x0000000000000000
payload_data_2 = 0x0000000000000000
instance_type = 0x0000000105ffc3f8
}
我可以在控制台中以什么方式查看我的变量,而不必筛选所有这些废话?
PS - 在这种情况下,我正在打印恰好是字典的Optional<Any>
对象,但它与非可选字典相同。
答案 0 :(得分:17)
expression debugPrint(object)
将上面的行放在调试器中并按Enter键。它将以更易读的格式打印出对象的内容。
你也可以使用另一个命令 - po print(data)
,这个命令更容易记住。
答案 1 :(得分:4)
⚠️ (以前)接受的答案仅将字典作为非格式的单行字符串提供,如下所示:
Optional(["transactionId": 333, "customerId": 111, "extraId": 444])
➡️只要获得更多密钥和嵌入式对象/词典,就很难阅读。
pjson
):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
别名,您可以在XCode的lldb终端中使用该别名:pjson object
let object: Any? = [
"customerId": 111,
"transactionId": 333,
"extraId": 444,
"embeddedDict": [
"foo": true
]
]
❌输出po print(data)
Optional(["transactionId": 333, "embeddedDict": ["foo": true], "customerId": 111, "extraId": 444])
✅输出pjson
{
"transactionId" : 333,
"embeddedDict" : {
"foo" : true
},
"customerId" : 111,
"extraId" : 444
}