struct ContentView: View {
@State var settingsConfiguration: Settings
struct Settings {
var passwordLength: Dou
If you are persisting a one-off struct such that a property wrapper is overkill, you can encode it as JSON. When decoding, use an empty Data
instance for the no-data case.
final class UserData: ObservableObject {
@Published var profile: Profile? = try? JSONDecoder().decode(Profile.self, from: UserDefaults.standard.data(forKey: "profile") ?? Data()) {
didSet { UserDefaults.standard.set(try? JSONEncoder().encode(profile), forKey: "profile") }
}
}