https://github.com/xueqooy/association
A swift library designed to dynamically associate values with objects at runtime
https://github.com/xueqooy/association
association cocoa objc-association objc-runtime swift
Last synced: 8 months ago
JSON representation
A swift library designed to dynamically associate values with objects at runtime
- Host: GitHub
- URL: https://github.com/xueqooy/association
- Owner: xueqooy
- License: mit
- Created: 2024-12-20T08:54:53.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-11T13:25:47.000Z (over 1 year ago)
- Last Synced: 2025-01-11T14:31:10.488Z (over 1 year ago)
- Topics: association, cocoa, objc-association, objc-runtime, swift
- Language: Swift
- Homepage:
- Size: 54.7 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Association
`Association` is a Swift library designed to dynamically associate values with objects at runtime.
## At a Glance
Extend UIView with an object.
```swift
class CustomObject {}
```
```swift
import Association
private let objectAssociation = Association()
extension UIView {
var customObject: CustomObject? {
get { Associations.objectAssociation[self] }
set { Associations.objectAssociation[self] = newValue }
}
}
```
comparison with Traditional Method:
```swift
import ObjectiveC.runtime
private var customObjectKey = "customObjectKey"
extension UIView {
var customObject: CustomObject? {
get {
objc_getAssociatedObject(
self,
&customObjectKey
) as? CustomObject
}
set {
objc_setAssociatedObject(
self,
&customObjectKey,
newValue,
.OBJC_ASSOCIATION_RETAIN_NONATOMIC
)
}
}
}
```
### Tips and Tricks
```swift
struct CustomStruct {}
class CustomObject {}
typealias Block = () -> Void
// By default, no wrapping is used, which corresponds to `none`.
private let objectAssociation = Association()
// Use `weak` wrapping for weakly referenced associative objects.
private let weakObjectAssociation = Association(wrap: .weak)
// It is recommended to use `direct` wrapping for custom value types.
// For types that can be bridged to Objective-C (e.g., String, Bool, Int), wrapping may not be necessary.
// Since Swift 3, custom value types are converted to `SwiftValue` in Objective-C, so wrapping may not be required.
private let structAssociation = Association(wrap: .direct)
// Closures should be associated using `direct` wrapping.
private let blockAssociation = Association(wrap: .direct)
extension UIView {
var customStruct: CustomStruct? {
get { structAssociation[self] }
set { structAssociation[self] = newValue }
}
var customObject: CustomObject? {
get { objectAssociation[self] }
set { objectAssociation[self] = newValue }
}
var weakCustomObject: CustomObject? {
get { weakObjectAssociation[self] }
set { weakObjectAssociation[self] = newValue }
}
var block: Block? {
get { blockAssociation[self] }
set { blockAssociation[self] = newValue }
}
}
```
## Installation
**Using [Swift Package Manager](https://swift.org/package-manager)**:
```swift
import PackageDescription
let package = Package(
name: "MyAwesomeApp",
dependencies: [
.Package(url: "https://github.com/xueqooy/Association", majorVersion: 1),
]
)
```
## License
**Association** is under MIT license. See the [LICENSE](LICENSE) file for more info.