Discover dump()


You’re more of a video kind of person? I’ve got you covered! Here’s a video with the same content than this article 🍿


Have you ever heard of the function dump()?

When you need to print a reference type to the console, this function can be quite useful!

Let’s take a look at this example:

In this code I’m trying to print the content of a reference type to the console.

When I use the function print(), I don’t get a very useful result…

But if instead I use the function dump(), then I get a much more interesting result!

Thanks to this little change, we now have all the fields of the reference type that get printed to the console 👌

So remember that whenever you’re dealing with a reference type like a class, the function dump() might be a much better choice than the function print()!

That’s all for this article, I hope you’ve enjoyed it!

Here’s the code if you want to experiment with it:

class Person {
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }

    var name: String
    var age: Int
}

let me = Person(name: "Vincent", age: 32)

dump(me)
Previous
Previous

Here’s another great learning resource 👌

Next
Next

85% of iOS developers got the wrong answer 🫢