A Swift utility library for type-safe storage and retrieval of custom types in NSUserDefaults.
TypedDefaults is a Swift utility library that enables type-safe interaction with NSUserDefaults, Apple's key-value storage system for iOS and macOS apps. It allows developers to store custom Swift types (like structs and enums) directly in NSUserDefaults without manual serialization, reducing boilerplate and preventing runtime type errors. The library provides a protocol-driven approach to define how types are saved and retrieved, ensuring compile-time safety.
iOS and macOS developers working with Swift who need to persistently store app settings or configuration data in a type-safe manner. It's particularly useful for developers who want to avoid the pitfalls of stringly-typed keys and manual type casting when using NSUserDefaults.
Developers choose TypedDefaults because it eliminates the risk of runtime crashes from incorrect type casting in NSUserDefaults, while also providing built-in support for dependency injection to make code more testable. Its lightweight, protocol-based design integrates seamlessly with Swift's type system without requiring heavy dependencies.
TypedDefaults is a utility library to type-safely use NSUserDefaults.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Ensures custom types stored in NSUserDefaults are checked at compile time, eliminating runtime errors from incorrect casting, as enforced by the DefaultsConvertible protocol.
Provides InMemoryStore and AnyStore for dependency injection, allowing unit testing by replacing persistent storage with in-memory mocks, as shown in the CameraViewController example.
Works with Swift structs, enums, and classes without requiring NSObject inheritance, promoting modern Swift patterns without Objective-C baggage.
The DefaultsConvertible protocol defines a standard way for serialization and deserialization, ensuring consistency across custom types in the codebase.
Each custom type must implement serialize() and init?(_ object:) methods manually, adding boilerplate code compared to automatic solutions like Codable.
Inherits constraints such as unsuitability for large data or complex queries, and lacks built-in encryption for sensitive information, limiting its use cases.
The library requires Swift 4.0 and Xcode 9, which may be outdated for newer projects, and users must handle updates or migration themselves without built-in tooling.