A Swift library providing type-safe associated objects with compile-time type checking and support for value types.
TSAO (Type-Safe Associated Objects) is a Swift library that provides a type-safe implementation of associated objects, bridging the gap between Objective-C's dynamic runtime and Swift's static type system. It solves the problem of untyped associated objects by ensuring compile-time type checking, supporting both reference and value types, and allowing configurable memory management policies without runtime overhead.
Swift developers working on iOS, macOS, watchOS, or tvOS who need to attach custom data to existing objects using associated objects but want type safety and modern Swift APIs.
Developers choose TSAO because it eliminates the risks of runtime type errors associated with traditional Objective-C associated objects, provides native Swift value type support, and offers a clean, expressive API with configurable policies while maintaining performance through compile-time optimizations.
Type-Safe Associated Objects in Swift
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Eliminates runtime casting by associating value types with keys at compile time, as highlighted in the README's key features, ensuring type errors are caught early.
Stores Swift value types like Int and structs through automatic boxing, overcoming Objective-C's object-only limitation, as described in the philosophy section.
Supports atomic/nonatomic retain, assign for classes, and copy for NSCopying types, providing flexible memory management options as detailed in the usage example.
Designed to allow the optimizer to remove type checks with zero runtime overhead, as noted in the README's footnote about compiler optimizations.
AssocMap values must be static or global; if discarded, they leak objects, as explicitly warned in the README's usage notes, requiring careful lifecycle management.
Boxing value types involves heap allocation, which can introduce performance costs in memory-sensitive applications, a trade-off admitted in the README's description of value type support.
Only works with AnyObject, limiting use in pure Swift code without Objective-C interoperability, which may not suit modern Swift-only projects.