UserDefaults Access via Property Wrappers
The hip and cool property-wrapper implementations on the web so far ignore the
registerDefaults
layer completely and provide a local fallback if no value for the key is persisted for the current user.The problem I see here is that you don’t have a shared default value anymore. Knowledge of the default value is local to the property declaration in a concrete type.
A full solution would include something like:
- A pair type that combines a key with a default value.
- A connection between that and
UserDefaults
for getting and setting values. - A way to access predefined pairs from different places in your code.
- A full list of the predefined pairs so that they can be registered.
- A way to link a property to a pair and a given instance of
UserDefaults
.