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...
Please Check my new post on CollectionOfOne in Swift: https://medium.com/p/collectionofone-in-swift-776591e8e8e4
For more visit: https://ioslifee.blogspot.com
Thanks for reading, Happy Coding 💻