Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. iOS
  3. TypedDefaults

TypedDefaults

MITSwift1.2.0

A Swift utility library for type-safe storage and retrieval of custom types in NSUserDefaults.

GitHubGitHub
110 stars3 forks0 contributors

What is TypedDefaults?

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.

Target Audience

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.

Value Proposition

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.

Overview

TypedDefaults is a utility library to type-safely use NSUserDefaults.

Use Cases

Best For

  • Storing app settings or user preferences with custom Swift types
  • Ensuring type safety when using NSUserDefaults in Swift projects
  • Unit testing code that depends on persistent storage by injecting in-memory stores
  • Reducing boilerplate code for serializing and deserializing custom types
  • Modernizing legacy Objective-C NSUserDefaults usage in Swift codebases
  • Building SwiftUI or UIKit apps that require type-safe configuration storage

Not Ideal For

  • Projects using Swift's Codable for automatic serialization of complex nested types
  • Applications requiring storage for large datasets or sensitive data beyond NSUserDefaults' capabilities
  • Teams preferring SwiftUI's @AppStorage for simple key-value pairs with zero boilerplate

Pros & Cons

Pros

Compile-Time Type Safety

Ensures custom types stored in NSUserDefaults are checked at compile time, eliminating runtime errors from incorrect casting, as enforced by the DefaultsConvertible protocol.

Enhanced Testability

Provides InMemoryStore and AnyStore for dependency injection, allowing unit testing by replacing persistent storage with in-memory mocks, as shown in the CameraViewController example.

Swift-Centric Design

Works with Swift structs, enums, and classes without requiring NSObject inheritance, promoting modern Swift patterns without Objective-C baggage.

Clear Protocol Contract

The DefaultsConvertible protocol defines a standard way for serialization and deserialization, ensuring consistency across custom types in the codebase.

Cons

Manual Serialization Overhead

Each custom type must implement serialize() and init?(_ object:) methods manually, adding boilerplate code compared to automatic solutions like Codable.

Tied to NSUserDefaults Limitations

Inherits constraints such as unsuitability for large data or complex queries, and lacks built-in encryption for sensitive information, limiting its use cases.

Potential Maintenance Burden

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.

Frequently Asked Questions

Quick Stats

Stars110
Forks3
Contributors0
Open Issues0
Last commit8 years ago
CreatedSince 2016

Tags

#ios#unit-testing#nsuserdefaults#dependency-injection#type-safety#utility-library#persistent-storage#cocoapods#swift

Built With

i
iOS
C
CocoaPods
S
Swift

Included in

iOS51.7k
Auto-fetched 10 hours ago

Related Projects

MMKVMMKV

An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, POSIX, and OHOS.

Stars18,727
Forks1,984
Last commit1 day ago
RealmRealm

Realm is a mobile database: a replacement for Core Data & SQLite

Stars16,613
Forks2,238
Last commit2 months ago
FMDBFMDB

A Cocoa / Objective-C wrapper around SQLite

Stars13,835
Forks2,731
Last commit5 months ago
WCDBWCDB

WCDB is a cross-platform database framework developed by WeChat.

Stars11,634
Forks1,499
Last commit5 months ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub