Member-only story
Using the @AppStorage Property Wrapper in SwiftUI
There are a few things when developing iOS apps that are simply unavoidable, and all developers deal with them sooner or later. One of those is saving and reading values to and from the user defaults file respectively.
User defaults is actually a property list file (plist) accessible programmatically that can store values of basic datatypes. Since it’s a property list file, stored content is in key-value pairs. The purpose of user defaults is to let us save bits of data and read them back whenever that’s necessary, quickly and effortlessly. This data is available app-wide and can be updated from anywhere inside an app. User defaults is not meant though to be acting as a persistent storage of large data. On the contrary, it’s best for keeping stuff like user preferences or settings. Also, it’s the worst choice a developer can make in order to save sensitive data. User defaults provide no security at all, and it’s easily accessible; so, just don’t do that.
In programming terms now, we all know from the pre-SwiftUI days that we can access user defaults using the UserDefaults
class in the Foundation framework. This is still valid nowadays as well. However, with the SwiftUI framework's coming, there is a new player around; t he @AppStorage
property wrapper.