I want to save the user settings in my app so when the app gets restarded the changes persist between runs. Mainly I would like to save the icon name (String) and the color theme (enum called Themes, described down below).
I’ve created a SwiftData model class called Settings:
`@Model
class Settings {
var icon: String
var theme: Themes
init(icon: String = "Default", theme: Themes = .blue) {
self.icon = icon
self.theme = theme
}
}that stores the name of the icon and the color theme of type Themes, which is the following enum:
extension Settings {
enum Themes: CaseIterable, Identifiable, Codable {
case red
case orange
case yellow
case pink
case blue // default
case green
case purple
case inverse
var id: Self { self }
func colorName () -> String {
...
// returns the selected color localized name
}
func colorValue () -> Color {
...
// returns the color
}
}
}`.
The value of icon and theme, even if changed, every time the app restarts, gets reset to the default value (the one seen in the initializer) and the data doesn’t persist.
I checked to find other solutions but nothing works.
Using XCode 15.4 (15F31d) – OS 17.5 (21F77) SDK + iOS 17.5 (21F79) Simulator (Installed)
Fil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.