SWIFT: “Dump” over “Print”

SWIFT: “Dump” over “Print”

Swift is the most intuitive and powerful language for the development of iOS, macOS, tvOS, watchOS, and beyond. Developing the Swift program is fun and interactive with an expressive and concise syntax. Swift comes with several modern features that businesses and developers love. While providing safe design and software, which runs lightning-fast, it includes other dynamic options beyond print () to display custom statements. 

Well everyone knows about print function to print a message at Xcode debug console. If we use Objective C then NSLog function is used to print a message at Xcode debug console.

print(“Hello World!!”)

NSLog(“Hello World!!”);

What if, you want to see the names and values of all the properties instead of just displaying some message? 

There is one more function which is available for printing a message at Xcode debug console called dump (available SDK Xcode 8.0+), which can be the right tool. 

So what’s the difference between print and dumb? The answer is when we print class objects, then print() simply outputs the class name while dump() outputs the whole class hierarchy.

Why use Dump over Print? 

The print is a simple and powerful method. It is generally enough to use print() in most of the places, but a problem is recognized. Though it works well for simple objects and values, when we use it for complex instance, the output is less informing. 

Example 1:

Suppose we have one variable

let message: NSString = "This is String."

Let us print and see how its looks

Code :

print(message)

Output :

This is String.

Now let us use dump and see how its look like

Code:

dump(message)

Output:

Output - Dump over Print

Example 2 :

Let’s see one more example of Class and objects

Suppose we have two classes:

Class and objects

Then, make-instance of Outer class

let objOuter = Outer(valueA: 100, valueY: "Y Have some value.", valueZ: 101.1540)

Let us print and see how its looks

Code :

print(objOuter)

Output :

YourProjetName.Outer

Let’s Start Using dump function

Code :

dump(objOuter)

Output :

Output - 2

Example 3 :

What if dump is used to console an array of Outer class objects?

Create 3 objects of Outer class and insert all into the Array object.

Code:

Code

Output :

Output 3

Syntax of dump

Syntax of dump

Let’s explore all Parameters one by one

1, Value - The value to output to the target stream

2, name - A label to use when writing the contents of value. When nil is passed, the label is omitted. The default is nil.

Let us dump and see how its looks

Code :

dump(objOuter, name: "Values of Outer Class Object")

Output:

Output 4

  1. indent - The number of spaces to use as an indent for each line of the output. The default is 0.

Code:

dump(objOuter, indent: 5)

Output:

output 4

4. maxDepth

The maximum depth to descend when writing the contents of a value that has nested components. The default is Int.max.

Code:

dump(objOuter, maxDepth: 0)

Output:

Output 8

Code:

dump(objOuter, maxDepth: 2)

Output:

output 5

Code:

dump(objOuter, maxDepth: 7)

Output:

output 6

  1. maxItems

The maximum number of elements, allowed to write the full contents. The default is Int.max.          

Code:

dump(objOuter, maxItems: 0)

Output:

// No Output Here

Code:

dump(objOuter, maxItems: 1)

Output:

Output 7

Code:

dump(objOuter, maxItems: 5)

Output:

output 9

and so on….

I hope this article has given you an idea about what can be possible with Dump and Print, which are available from Swift. Learn and use these kinds of dynamic and debugging features in Swift to develop clean code, ensure code clarity as well as resolve unforeseen problems quickly.

Thanks for reading, Happy Coding.

Author

Talk To Our Experts