Updated 27 August 2021
Swift comes with several modern features every year in the market which is the reason for the popularity of Apple. Swift programming, everyone knows about the print function to print the message at Xcode debug console and in Objective C we used the NSLog function to print the message at the console. In this blog, we are discussing how to print all properties of the values instead of just displaying the name on the message.
Xcode 8.0+, the Dump is introducing for printing the messages at the Xcode Debug console. Dumps the given object’s contents using its mirror to standard output and specified output stream.
We can use also Dump for the same purpose as we used to print() but print() only prints the class name while in the case of dump() prints the whole class hierarchy at Xcode debug console.
1 |
func dump<T>(T, name: String?, indent: Int, maxDepth: Int, maxItems: Int) -> T |
1 |
func dump<T, TargetStream>(T, to: inout TargetStream, name: String?, indent: Int, maxDepth: Int, maxItems: Int) -> T |
Let’s suppose we have 3 classes SchoolRegister, TeacherRegister and StudentRegister.
SchoolRegister class makes two instances of TeacherRegister and StudentRegister class as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class SchoolRegister { var teacherRegister = TeacherRegister() var schoolRegister = StudentRegister() } class TeacherRegister{ var total = 10 var absent = 2 var present = 8 } class StudentRegister{ var total = 10 var absent = 2 var present = 8 } |
Now takes an instance of SchoolRegister class as;
1 |
let schoolRegister = SchoolRegister() |
Firstly, we checked with print() function as:
1 |
print(schoolRegister) |
Now we have checked with Dump() function as:
1 |
dump(schoolRegister) |
Also, check with other parameters of dump() :
1 |
dump(schoolRegister, name: "School Main Register") |
1 2 3 |
dump(schoolRegister, maxDepth: 1) dump(schoolRegister, maxDepth: 2) dump(schoolRegister, maxDepth: 0) |
Used to show max items of hierarchy in the debug console
1 2 3 |
dump(schoolRegister, maxItems: 1) dump(schoolRegister, maxItems: 2) dump(schoolRegister, maxItems: 0) |
If you have any comments, questions, or recommendations, feel free to post them in the comment section below!
For other blogs, please click here.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.