CaptureContext/swift-associated-objects
Associated objects utils
Table of contents
- Basic - Recommended
Usage
Basic helpers for object association are available in "AssociatedObjects" product
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
@AssociatedObjectmacro uses.retain(.nonatomic)for classes and.copy(.nonatomic)objc_AssociationPolicyfor structs.
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.
- From the File menu, select Swift Packages › Add Package Dependency…
- Enter
"https://github.com/capturecontext/swift-associated-objects.git"into the package repository URL text field - 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.
.package(
url: "https://github.com/capturecontext/swift-associated-objects.git",
.upToNextMinor(from: "0.1.4")
)Do not forget about target dependencies:
.product(
name: "AssociatedObjects",
package: "swift-associated-objects"
).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 for details.
Package Metadata
Repository: CaptureContext/swift-associated-objects
Stars: 1
Forks: 0
Open issues: 0
Default branch: main
Primary language: swift
License: MIT
Topics: associatedobject, macro, macros, objc, runtime, swift-macros
README: README.md