Swift Language Debug Print


Example

Debug Print shows the instance representation that is most suitable for debugging.

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

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

print(dict)
debugPrint(dict)

Yields

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

This extra information can be very important, for example:

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

print(wordArray)
debugPrint(wordArray)

Yields

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

Notice how in the first output it appears that there are 4 elements in the array as opposed to 3. For reasons like this, it is preferable when debugging to use debugPrint

Updating a classes debug and print values

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