Skip to main content

Module defaults

Module defaults 

Source
Expand description

UserDefaults, through the Objective-C runtime.

The store behind @AppStorage, reached directly. SwiftUI’s own property wrapper cannot be driven from here — its wrappedValue accessor resolves a canonical location against the update graph it was installed in, so calling it outside a view body faults inside UserDefaultLocation.canonicalLocation no matter how the call is spelled (measured against four conventions; the construction works, the accessor does not). Installing one properly means giving the run-time-synthesised view type an AppStorage field so SwiftUI calls _makeProperty on it, which is how @State is already done here.

What that wrapper is, though, is a value in UserDefaults plus a re-render when it changes — and both halves are already available: this module for the store, and BoundState for the binding SwiftUI reads. So persistence is spelled BoundState::persistent("key", default), and every control that takes a binding persists with no further work.

Traits§

Persisted
A type UserDefaults can hold, and that a binding can therefore persist.

Functions§

has
Is anything stored under key?
remove
Forget key, so the next read falls back.