Xcode中更漂亮的调试输出打印Swift字典 [英] Prettier debug output printing Swift Dictionary in Xcode
问题描述
当我在Swift的字典中使用 print()
时,它在控制台中变得非常漂亮,带有一个键和一个值。
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 = 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
}
我可以在控制台中执行哪些操作以查看我的变量
What can I do in the console to see my variables in a way that I can actually read them without having to sift through all this nonsense?
PS-在这种情况下,我正在打印 Optional< Any>
对象恰好是字典,但与非可选字典相同。
PS - In this case I am printing an Optional<Any>
object that happens to be a dictionary, but it's the same with a non-optional Dictionary.
推荐答案
⚠️ (先前)接受的答案仅将字典作为非格式的单行字符串提供,如下所示:
⚠️ 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.
- 要获得不错的json格式(缩进,换行等),您可以定义一个别名(我将其命名为
pjson
)通过在lldb终端中运行此命令(源):
- To get a nice json formatting (indentations, newlines, etc) you can define an alias (I named mine
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)!))/'
- 您可能不想每次打开XCode时都重新定义别名,因此运行以下命令将别名定义附加到
〜/ .lldbinit
: - You probably don't want to re-define the alias everytime you open XCode, so run the following command to append the alias definition to
~/.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终端中使用c $ c>别名:
- This will create the
pjson
alias which you can use in your lldb terminal in XCode:
pjson object
比较以下Swift对象的输出:
let object: Any? = [
"customerId": 111,
"transactionId": 333,
"extraId": 444,
"embeddedDict": [
"foo": true
]
]
❌输出 po print(data)
❌ Output of po print(data)
Optional(["transactionId": 333, "embeddedDict": ["foo": true], "customerId": 111, "extraId": 444])
✅ pjson
✅ Output of pjson
{
"transactionId" : 333,
"embeddedDict" : {
"foo" : true
},
"customerId" : 111,
"extraId" : 444
}
这篇关于Xcode中更漂亮的调试输出打印Swift字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!