I have created a custom class (as an ObservedObject since I need to share this data). Everything works but when I force close the app the data resets and I have not found any way to save it to UserDefaults. (Trying to save normally as an object crashed the app).
How can I save this data? (When pressing the button in SettingsView)
First time asking here.
---MainView---
import SwiftUI
class ProgressData: ObservableObject {
@Published var licenseDate: Date = Date()
@Published var dayProgressValue: Float = 0.00
@Published var nightProgressValue: Float = 0.00
@Published var newDriverProgressValue: Float = 0.00
}
struct MainView: View {
let defualts = UserDefaults.standard
@ObservedObject var data = ProgressData()
var body: some View {
// App Code Here!
}
--- SettingsView ---
import SwiftUI
struct SettingsView: View {
@ObservedObject var data: ProgressData
var body: some View {
Button(action: {
// What to do in order to save it here?
})
}
Thanks.