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