forwarding proxy propertywrapper

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-07-29 16:06:32 -05:00
parent 443bbded79
commit 13b5ea2a55
2 changed files with 51 additions and 0 deletions

View File

@ -40,6 +40,7 @@
EA3362432892EFF20071C351 /* VDSLabelModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA3362422892EFF20071C351 /* VDSLabelModel.swift */; };
EA3362452892F9130071C351 /* Labelable.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA3362442892F9130071C351 /* Labelable.swift */; };
EA33624728931B050071C351 /* Initable.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA33624628931B050071C351 /* Initable.swift */; };
EA3C3B4C2894823E000CA526 /* ProxPropertyWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA3C3B4B2894823E000CA526 /* ProxPropertyWrapper.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -87,6 +88,7 @@
EA3362422892EFF20071C351 /* VDSLabelModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VDSLabelModel.swift; sourceTree = "<group>"; };
EA3362442892F9130071C351 /* Labelable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Labelable.swift; sourceTree = "<group>"; };
EA33624628931B050071C351 /* Initable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Initable.swift; sourceTree = "<group>"; };
EA3C3B4B2894823E000CA526 /* ProxPropertyWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxPropertyWrapper.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -213,6 +215,7 @@
isa = PBXGroup;
children = (
EA3361B5288B2A410071C351 /* VDSControl.swift */,
EA3C3B4B2894823E000CA526 /* ProxPropertyWrapper.swift */,
);
path = Classes;
sourceTree = "<group>";
@ -387,6 +390,7 @@
EA3362432892EFF20071C351 /* VDSLabelModel.swift in Sources */,
EA33624728931B050071C351 /* Initable.swift in Sources */,
EA3361BD288B2C760071C351 /* TypeAlias.swift in Sources */,
EA3C3B4C2894823E000CA526 /* ProxPropertyWrapper.swift in Sources */,
EA3361AF288B26310071C351 /* FormFieldable.swift in Sources */,
EA3361B3288B265D0071C351 /* Changable.swift in Sources */,
EA336171288B19200071C351 /* VDS.docc in Sources */,

View File

@ -0,0 +1,47 @@
//
// ProxPropertyWrapper.swift
// VDS
//
// Created by Matt Bruce on 7/29/22.
//
import Foundation
//https://gist.github.com/jegnux/4a9871220ef93016d92194ecf7ae8919#file-proxypropertywrapper-swift
@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
}
}
}
// Kudos @johnsundell for this trick
// https://swiftbysundell.com/articles/accessing-a-swift-property-wrappers-enclosing-instance/
extension NSObject: ProxyContainer {}
public protocol ProxyContainer {
typealias Proxy<T> = AnyProxy<Self, T>
}