Dump in Swift

We all know that we use print function to print a message into the Xcode debug console

print("Hello world")

We can use also Dump for same propose but print() simply outputs the class name and dump() outputs the whole class hierarchy.

Suppose you have a class

class Employee {
let name = “Pawan”
let department = “iOS”
let ids = [1, 2, 3]
}

then make an instance of that class

let emp = Employee()

first, let us use print and see how it’s looks

print(emp) //YourProjectName.Employee

let’s start using dump

dump(emp)//Output
YourProjectName.Employee #0
- name: “Pawan”
- department: “iOS”
▿ ids: 3 elements
- 1
- 2
- 3

Syntax of dump

@discardableResult func dump<T>(_ value: T, name: String? = nil, indent: Int = 0, maxDepth: Int = .max, maxItems: Int = .max) -> T

let’s explore all the dump’s parameters

1, name

It helps to print any prefix string before the output

dump(emp, name: "hi")//Output
hi:
YourProjectName.Employee #0
- name: “Pawan”
- department: “iOS”
▿ ids: 3 elements
- 1
- 2
- 3

2, indent

It helps to provide space before the output

dump(emp, indent: 3)//Output
▿ YourProjectName.Employee #0
- name: "Pawan"
- department: "iOS"
▿ ids: 3 elements
- 1
- 2
- 3

3, maxDepth

It helps to go in-depth of the output

dump(emp, maxDepth: 0)//Output
YourProjectName.Employee #0
dump(emp, maxDepth: 1)//Output
YourProjectName.Employee #0
- name: "Pawan"
- department: "iOS"
▹ ids: 3 elements
dump(emp, maxDepth: 2)//Output
YourProjectName.Employee #0
- name: "Pawan"
- department: "iOS"
▿ ids: 3 elements
- 1
- 2
- 3

3, maxItems

It helps to show max items in the debug console

dump(emp, maxItems: 0)//No Output here
dump(emp, maxItems: 1)//Output
YourProjectName.Employee #0
(3 children)
dump(emp, maxDepth: 2)//Output
YourProjectName.Employee #0
- name: "Pawan"
(2 more children)

dump
(emp, maxDepth: 3)
//Output
YourProjectName.Employee #0
- name: "Pawan"
- department: "iOS"
(1 more child)
dump(emp, maxDepth: 4)//Output
YourProjectName.Employee #0
- name: "Pawan"
- department: "iOS"
▿ ids: 3 elements
(3 more children)
dump(emp, maxDepth: 5)//Output
YourProjectName.Employee #0
- name: "Pawan"
- department: "iOS"
▿ ids: 3 elements
- 1
(2 more children)
and so on...

For more visit: https://ioslifee.blogspot.com

Thanks for reading, Happy Coding 💻

iOS Developer(https://ioslifee.blogspot.com)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

✅Bifrost x BiFi Lunar New Year Event ✅Event Duration: January 27 — February 14 💰Reward: Upto 1,000…

Using Swagger with Tornado

Saving and Reloading Data with File Serialization in Java

Leetcode- Kth Largest Element in an Array using Quick Select

JSON serialization: Ignore selective properties or null properties

BlueBitFinance IDO announcement

An overnight success, 10 years in the making ….

Security Onion CrowdStrike Connector Installation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Manjani Pavan

Manjani Pavan

iOS Developer(https://ioslifee.blogspot.com)

More from Medium

A swift introduction to SWIFT

SOLID Principles in Swift

swift 001

Wordle Game on iOS (Cordle)