Site icon Ravi Shankar

How to simplify persistent data using @AppStorage

In SwiftUI, the @AppStorage property wrapper that assists how we interact with UserDefaults, streamlining the process of persisting data. By simplifying data persistence, @AppStorage makes reading from and writing to UserDefaults more straightforward and integrated within the SwiftUI framework.

Let us take the following example where we are trying to store tap count in User Defaults. Typically the code to read from User Defaults will look like this

let tapCount = UserDefaults.standard.integer(forKey: "Tap")

And code like this to write to UserDefaults

UserDefaults.standard.set(tapCount, forKey: "Tap")

You can replace the above code with @AppStorage for a more SwiftUI integrated approach. To do this, declare a property in your view like

@AppStorage("Tap") var tapCount: Int = 0

This does a few things for you:

Here is an example of it in use within a view:

struct TapCounterView: View {
    @AppStorage("Tap") var tapCount: Int = 0

    var body: some View {
        Button("Tap me!") {
            tapCount += 1
        }
        Text("Tap count: \(tapCount)")
    }
}

Exit mobile version