Last active
June 26, 2024 20:32
-
-
Save jegnux/4a9871220ef93016d92194ecf7ae8919 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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> | |
} |
🙇♂️
@JohnSundell wrote a blog post about this trick and adds a really nice trick to avoid type repetition in the key path:
https://swiftbysundell.com/articles/accessing-a-swift-property-wrappers-enclosing-instance/
Please consider a unavailable
to wrappedValue, it makes it more friendly since it provides a build Error for accidentally put Proxy
in a struct.
@available(*, unavailable, message: "Proxy should be in a class")
public var wrappedValue: Value {
get { fatalError() }
set { fatalError() }
}
@ytyubox that's a good idea thanks :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
💯