Skip to content

Instantly share code, notes, and snippets.

@Fiser12
Forked from jegnux/ProxyPropertyWrapper.swift
Created March 4, 2023 15:50
Show Gist options
  • Save Fiser12/f72c538a962fb61152e5807de0aae28a to your computer and use it in GitHub Desktop.
Save Fiser12/f72c538a962fb61152e5807de0aae28a to your computer and use it in GitHub Desktop.
@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>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment