A Swift package that wraps UserDefaults with typed keys, default values, and optional iCloud sync. It suits app storage across Apple platforms and SwiftUI.
Overview
Defaults is a Swift package that stores key-value pairs across app launches on top of the system UserDefaults API. It offers a type-safe facade so you declare keys with types and defaults in one place instead of scattering string keys and fallback values.
You access values through a typed subscript and can observe key changes with async streams or callbacks. SwiftUI integration uses property wrappers that refresh views when stored values change, and a Toggle helper binds to boolean keys. Data is stored as JSON-serialized values for easier debugging.
The package supports many built-in types including Codable and NSSecureCoding, and you can add custom types through serialization protocols and bridges. An iCloud module can synchronize values across devices.
Install it as a Swift Package Manager dependency in Xcode. It targets macOS, iOS, tvOS, watchOS, and visionOS according to the compatibility notes in the source material.
Key features
- Strongly typed keys with centralized defaults
- SwiftUI property wrappers and Toggle
- Change observation via async streams
- JSON-serialized storage for debugging
- Codable and NSSecureCoding support
- Custom type bridges and serialization
- Optional iCloud cross-device sync
- Usable outside SwiftUI views
Use cases
- App preference and settings storage
- SwiftUI state backed by persistence
- Shared typed keys across app modules
- Custom Codable types in UserDefaults
- Multi-device settings via iCloud