vds_ios/VDS/PropertyWrappers/AnyProxy.swift
Matt Bruce d0b2dfd0b1 refactored propertywrappers
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-08-11 15:12:14 -05:00

46 lines
1.4 KiB
Swift

//
// ProxPropertyWrapper.swift
// VDS
//
// Created by Matt Bruce on 7/29/22.
//
import Foundation
//https://www.swiftbysundell.com/articles/accessing-a-swift-property-wrappers-enclosing-instance/
@propertyWrapper
public struct AnyProxy<EnclosingSelf, Value> {
private let keyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>
public init(_ keyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>) {
self.keyPath = keyPath
}
@available(*, unavailable, message: "The wrapped value must be accessed from the enclosing instance property.")
public var wrappedValue: Value {
get { fatalError() }
set { fatalError() }
}
public static subscript(
_enclosingInstance observed: EnclosingSelf,
wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>,
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self>
) -> Value {
get {
let storageValue = observed[keyPath: storageKeyPath]
let value = observed[keyPath: storageValue.keyPath]
return value
}
set {
let storageValue = observed[keyPath: storageKeyPath]
observed[keyPath: storageValue.keyPath] = newValue
}
}
}
extension NSObject: ProxyContainer {}
public protocol ProxyContainer {
typealias Proxy<T> = AnyProxy<Self, T>
}