I want to keep track of my UserDefaults in my View model. To retrieve and add from my UserDefault, I currently do this.
extension UserDefaults {
var ratedProducts: [Product] {
get {
guard let data = UserDefaults.standard.data(forKey: "ratedProducts") else { return [] }
return (try? PropertyListDecoder().decode([Product].self, from: data)) ?? []
}
set {
UserDefaults.standard.set(try? PropertyListEncoder().encode(newValue), forKey: "ratedProducts")
}
}
}
But now what I want to do is, having a published computed property in my View model that keeps track whenever an object is added. Which then should trigger an update in my progress bar within my view. How would I setup this property properly?
I thought of something like this. (Obviously this doesnt work)
@Published var ratedProducts: [Product] {
ratedProducts = UserDefaults.standard.ratedProducts
}
Thanks for any help!