Whenever the value of the @State variable myData changes I would like to be notified and store that data in an @AppStorage variable myStoredData. However, I don't want to have to write explicitly this storing code everywhere the state var is changed, I would like to associate a block of code with it that gets notified whenever the state var changes and performs storage. The reason for this is, for example, I want to pass the state var as a binding to another view, and when that view would change the variable, the storage block would automatically be executed. How can I do this/can I do this in SwiftUI?
struct MyView : View {
@AppStorage("my-data") var myStoredData : Data!
@State var myData : [String] = ["hello","world"]
var body : some View {
Button(action: {
myData = ["something","else"]
myStoredData = try? JSONEncoder().encode(myData)
}) {
Text("Store data when button pressed")
}
.onAppear {
myData = (try? JSONDecoder().decode([String].self, from: myStoredData)) ?? []
}
}
}
I'm looking for something like this, but this does not work:
@State var myData : [String] = ["hello","world"] {
didSet {
myStoredData = try? JSONEncoder().encode(myData)
}
}