傾倒
dump 通過反射(映象)列印物件的內容。
陣列的詳細檢視:
let names = ["Joe", "Jane", "Jim", "Joyce"]
dump(names)
列印:
▿4 個元素
- [0]:喬
- [1]:簡
- [2]:吉姆
- [3]:喬伊斯
對於字典:
let attributes = ["foo": 10, "bar": 33, "baz": 42]
dump(attributes)
列印:
▿3 個鍵/值對
▿[0] :( 2 個元素)
- .0:bar
- .1:
33▿[1] :( 2 個元素)
- .0:baz
- .1:
42▿[2] :( 2 個元素)
- .0:foo
- .1:10
dump 被宣告為 dump(_:name:indent:maxDepth:maxItems:)。
第一個引數沒有標籤。
還有其他可用的引數,例如 name 為被檢查物件設定標籤:
dump(attributes, name: "mirroring")
列印:
▿映象:3 個鍵/值對
▿[0] :( 2 個元素)
- .0:bar
- .1:
33▿[1] :( 2 個元素)
- .0:baz
- .1:
42▿[2] :(2 個元素)
- .0:foo
- .1:10
你也可以選擇僅使用 maxItems:列印一定數量的專案,使用 maxDepth:將物件解析到一定深度,並使用 indent:更改列印物件的縮排。