mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-27 23:24:32 +00:00
28 lines
842 B
Swift
28 lines
842 B
Swift
//
|
|
// AssociatedObject.swift
|
|
// DynamicIsland
|
|
//
|
|
// Helper for retaining objects via Objective-C associated storage.
|
|
//
|
|
|
|
import Foundation
|
|
import ObjectiveC
|
|
|
|
public struct AssociatedObject<Value: AnyObject> {
|
|
private let key: UnsafeRawPointer
|
|
private let policy: objc_AssociationPolicy
|
|
|
|
public init(_ policy: objc_AssociationPolicy = .OBJC_ASSOCIATION_RETAIN_NONATOMIC) {
|
|
self.key = UnsafeRawPointer(Unmanaged.passUnretained(UniqueKey()).toOpaque())
|
|
self.policy = policy
|
|
}
|
|
|
|
private final class UniqueKey {}
|
|
|
|
public subscript<Owner: AnyObject>(_ owner: Owner) -> Value? {
|
|
get { objc_getAssociatedObject(owner, key) as? Value }
|
|
nonmutating set { objc_setAssociatedObject(owner, key, newValue, policy) }
|
|
}
|
|
}
|
|
|
|
extension AssociatedObject: @unchecked Sendable where Value: Sendable {}
|