https://github.com/CaptureContext/swift-associated-objects
Associated objects utils
https://github.com/CaptureContext/swift-associated-objects
associatedobject macro macros objc runtime swift-macros
Last synced: about 1 month ago
JSON representation
Associated objects utils
- Host: GitHub
- URL: https://github.com/CaptureContext/swift-associated-objects
- Owner: CaptureContext
- License: mit
- Created: 2025-07-28T14:38:12.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2026-02-07T16:27:53.000Z (2 months ago)
- Last Synced: 2026-02-08T00:52:18.885Z (2 months ago)
- Topics: associatedobject, macro, macros, objc, runtime, swift-macros
- Language: Swift
- Homepage:
- Size: 31.3 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# swift-associated-objects
[](https://github.com/capturecontext/swift-associated-objects/actions/workflows/ci.yml) [](https://swiftpackageindex.com/capturecontext/swift-associated-objects) [](https://swiftpackageindex.com/capturecontext/swift-associated-objects)
Associated objects utils
## Table of contents
- [Documentation](https://swiftpackageindex.com/CaptureContext/swift-associated-objects/0.5.0/documentation/associatedobjects)
- [Usage](#usage)
- [Installation](#installation)
- [Basic](#basic)
- [Recommended](#recommended)
- [Licence](#licence)
## Usage
Basic helpers for object association are available in "AssociatedObjects" product
```swift
extension UIViewController {
var someStoredProperty: Int {
get { getAssociatedObject(forKey: #function).or(0) }
set { setAssociatedObject(newValue, forKey: #function) }
}
}
let value: Bool = getAssociatedObject(forKey: "value", from: object)
```
But the full power of associated objects is provided by `AssociatedObjectsMacros` target
> By default `@AssociatedObject` macro uses `.retain(.nonatomic)` for classes and `.copy(.nonatomic)` `objc_AssociationPolicy` for structs.
```swift
import AssociatedObjectsMacros
extension SomeClass {
@AssociatedObject
var storedVariableInExtension: Int = 0
@AssociatedObject(readonly: true)
var storedVariableInExtension: SomeObject = .init()
@AssociatedObject
var optionalValue: Int?
@AssociatedObject
var object: Int?
@AssociatedObject(threadSafety: .atomic)
var threadSafeValue: Int?
@AssociatedObject(threadSafety: .atomic)
var threadSafeObject: Object?
@AssociatedObject(policy: .assign)
var customPolicyValue: Int?
@AssociatedObject(policy: .retain(.atomic))
var customPolicyThreadSafeObject: Object?
}
```
> Macros require swift-syntax compilation, so it will affect cold compilation time
## Installation
### Basic
You can add AssociatedObjects to an Xcode project by adding it as a package dependency.
1. From the **File** menu, select **Swift Packages › Add Package Dependency…**
2. Enter [`"https://github.com/capturecontext/swift-associated-objects.git"`](https://github.com/capturecontext/swift-associated-objects.git) into the package repository URL text field
3. Choose products you need to link them to your project.
### Recommended
If you use SwiftPM for your project, you can add AssociatedObjects to your package file.
```swift
.package(
url: "https://github.com/capturecontext/swift-associated-objects.git",
.upToNextMinor(from: "0.1.4")
)
```
Do not forget about target dependencies:
```swift
.product(
name: "AssociatedObjects",
package: "swift-associated-objects"
)
```
```swift
.product(
name: "AssociatedObjectsMacros",
package: "swift-associated-objects"
)
```
> [!NOTE]
>
> _The package is compatible with non-Apple platforms, but this package uses conditional compilation, so APIs are only available `#if canImport(ObjectiveC)`_
## License
This library is released under the MIT license. See [LICENSE](LICENSE) for details.