Friday, December 13, 2019 [Tweets] [Favorites]

UserDefaults Access via Property Wrappers

Christian Tietze:

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:

  1. A pair type that combines a key with a default value.
  2. A connection between that and UserDefaults for getting and setting values.
  3. A way to access predefined pairs from different places in your code.
  4. A full list of the predefined pairs so that they can be registered.
  5. A way to link a property to a pair and a given instance of UserDefaults.

Comments

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment