talira

Defaults

Swifty and modern UserDefaults

open sourceMITfree

Defaults is Sindre Sorhus's Swift package for storing key-value settings across app launches. It wraps Apple's UserDefaults in a type-safe API: each key is declared once with its type and default value, any Codable or NSSecureCoding value can be stored, changes can be observed, a SwiftUI property wrapper updates views when a value changes, and values can sync between devices through iCloud. Its README says it runs in production in its author's apps.

Problems it solves

  • store user settings in my Swift app with type-safe keys and default values
  • save a Codable enum in UserDefaults without writing encoding code
  • update my SwiftUI view automatically when a stored setting changes
  • sync app preferences between a user's Mac and iPhone through iCloud

Features

  • strongly typed keys with default values declared once
  • Codable and NSSecureCoding values
  • a SwiftUI property wrapper and Toggle component
  • observation of key changes
  • iCloud sync between devices
  • values stored as JSON for debugging