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!
โค๏ธ