除錯列印

Debug Print 顯示最適合除錯的例項表示。

print("Hello")
debugPrint("Hello")

let dict = ["foo": 1, "bar": 2]

print(dict)
debugPrint(dict)

輸出

>>> Hello
>>> "Hello"
>>> [foo: 1, bar: 2]
>>> ["foo": 1, "bar": 2]

這些額外資訊非常重要,例如:

let wordArray = ["foo", "bar", "food, bars"]

print(wordArray)
debugPrint(wordArray)

輸出

>>> [foo, bar, food, bars]
>>> ["foo", "bar", "food, bars"]

請注意,在第一個輸出中,如果陣列中有 4 個元素而不是 3 個元素。出於這樣的原因,最好在除錯時使用 debugPrint

更新類除錯和列印值

struct Foo: Printable, DebugPrintable {
    var description: String {return "Clear description of the object"}
    var debugDescription: String {return "Helpful message for debugging"}
}

var foo = Foo()

print(foo)
debugPrint(foo)

>>> Clear description of the object
>>> Helpful message for debugging