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:
- It automatically reads the current value of
"Tap"
fromUserDefaults
and assigns it totapCount
. - Whenever
tapCount
changes, it automatically updates the value inUserDefaults
. - You have specified a default value of
0
, which means if"Tap"
hasn’t been set inUserDefaults
yet,tapCount
will default to0
.
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)") } }
Leave a Reply