gpt4 book ai didi

swift - 更漂亮的调试输出在 Xcode 中打印 Swift 字典

转载 作者:搜寻专家 更新时间:2023-10-30 22:03:56 25 4
gpt4 key购买 nike

当我使用 print() 时在 Swift 中的字典上,它在控制台中非常漂亮,带有一个键和一个值。

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>对象恰好是字典,但它与非可选字典相同。

最佳答案

新答案(2021):

获得可读输出的最快方法是使用:

po 打印(数据)

比如说,你有如下变量data:

let data: [String: Any] = ["value1": 64, "value2": true, "value3": "some"]

当你执行 po print(data) 时,你将得到以下输出:

(lldb) po print(data)
["value1": 64, "value2": true, "value3": "some"]
0 elements

如果您不着急,可以按照 below 中的答案中的步骤改进调试打印格式。 .

旧答案(2017 年):

expression debugPrint(object)

just put the line above in your debugger and hit enter. It will printout contents of our object in more human readable format.

also you can use another one command - po print(data), which iseasier to remember.

关于swift - 更漂亮的调试输出在 Xcode 中打印 Swift 字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42236555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com