Skip to content

Instantly share code, notes, and snippets.

@StewartLynch
Last active October 10, 2023 02:39
Show Gist options
  • Save StewartLynch/ae8efa87a15f9efce48b41350080809a to your computer and use it in GitHub Desktop.
Save StewartLynch/ae8efa87a15f9efce48b41350080809a to your computer and use it in GitHub Desktop.
@Observable class SomeClass {
// @AppStorage("name") var name = "" // Does not work
/// This will do the same thing
var name: String {
get {
access(keyPath: \.name)
return UserDefaults.standard.string(forKey: "name") ?? ""
}
set {
withMutation (keyPath: \.name) {
UserDefaults.standard.setValue (newValue, forKey: "name")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment