Did you know Property Wrappers are great debugging tools? 🐛

Hi 👋

I don’t know about you, but I always find it particularly satisfactory when I discover a clever new use case of a well known tool!

And the topic of this email is definitely an instance of that!

I’m sure you’ve already heard of Property Wrappers.

These annotations have been part of Swift for almost 4 years and they become ubiquitous as soon as start to you write SwiftUI code: @State, @Binding, @Published, etc.

But did you know that Property Wrappers can also be used as debugging tools? 🐛

Let me explain!

Most of the time, we deal with property wrappers that are meant to become part of our production code: remove an @Published from a property and your code no longer works as intended.

But what about a property wrapper that would only be added during a debug session and that would be removed just after? 😌

Just like a breakpoint basically.

In this email I want to show you that such a property wrapper can be quite useful!

Consider the following code:

Once we’ve set a new value to user.name, it’s impossible to access the previous one, even through a breakpoint.

However, there are instances when knowing the previous values of a property can make a debugging session much easier!

What if we implemented a property wrapper that does exactly that?

As you can see, nothing fancy in the implementation: the property wrapper simply maintains an array of the previous values 👌

Now we can simply annotate a property with @Versioned and we immediately get access to all of its previous values 😌

Of course, don’t forget to remove @Versioned once you’re done with you’re debugging 😅

I hope you’ve enjoyed learning this nice little trick and that maybe it will come to your rescue one of these days!

This email was inspired by one of my first videos, here’s the link if you want to check all the progress I’ve made since then 😛

That’s all for this email, thanks for reading it!

If you’ve enjoyed it, feel free to forward it
to your friends and colleagues 🙌

I wish you an amazing week!

❤️

Previous
Previous

85% of iOS developers got the wrong answer 🫢

Next
Next

Discover how @MainActor works