@AppStorage Property Wrapper Explained
Whenever we need to save some basic, not secure data, such as settings for an app, we refer to UserDefaults
. However, SwiftUI comes with the @AppStorage
property wrapper that allows us to read and write data with UserDefaults in a very easy and elegant way. In the present article let’s cover what the @AppStorage
property wrapper is and what it does.
As mentioned previously, the @AppStorage
property wrapper allows us to read and write data using UserDefaults
. It also watches a key for changes, and if that happens it reloads the view’s body to update UI.
Consider the following example, where a user can choose a dark or light mode in the app, and it will be saved, updated if needed, and retrieved when the app launches.
It’s very easy to use this powerful property wrapper. The only thing You should not forget is not to save any secure or personal data using it.
To sum up, when we mark a property as @AppStorage
it can write data using UserDefaults
.
Thanks for reading!
if you enjoyed this article, be sure to give it a clap so others will find it more easily.