调试打印

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