From 13b5ea2a559588ce5599fe9cb014f6e196839176 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 29 Jul 2022 16:06:32 -0500 Subject: [PATCH] forwarding proxy propertywrapper Signed-off-by: Matt Bruce --- VDS.xcodeproj/project.pbxproj | 4 +++ VDS/Classes/ProxPropertyWrapper.swift | 47 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 VDS/Classes/ProxPropertyWrapper.swift diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index 326e6999..46e33dda 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -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 = ""; }; EA3362442892F9130071C351 /* Labelable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Labelable.swift; sourceTree = ""; }; EA33624628931B050071C351 /* Initable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Initable.swift; sourceTree = ""; }; + EA3C3B4B2894823E000CA526 /* ProxPropertyWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxPropertyWrapper.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -213,6 +215,7 @@ isa = PBXGroup; children = ( EA3361B5288B2A410071C351 /* VDSControl.swift */, + EA3C3B4B2894823E000CA526 /* ProxPropertyWrapper.swift */, ); path = Classes; sourceTree = ""; @@ -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 */, diff --git a/VDS/Classes/ProxPropertyWrapper.swift b/VDS/Classes/ProxPropertyWrapper.swift new file mode 100644 index 00000000..17784723 --- /dev/null +++ b/VDS/Classes/ProxPropertyWrapper.swift @@ -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 { + private let keyPath: ReferenceWritableKeyPath + + public init(_ keyPath: ReferenceWritableKeyPath) { + 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, + storage storageKeyPath: ReferenceWritableKeyPath + ) -> 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 = AnyProxy +}