Skip to content

Instantly share code, notes, and snippets.

@Adobels
Created July 7, 2024 20:52
Show Gist options
  • Save Adobels/2dea07c9f7e65a1127cde7211ce3394a to your computer and use it in GitHub Desktop.
Save Adobels/2dea07c9f7e65a1127cde7211ce3394a to your computer and use it in GitHub Desktop.
Draft: Swift @propertyWrapper to execute given selector on value did change. Can be helpfull with UIViewKit
@propertyWrapper
public struct InvalidateBody<T> {
private var value: T
weak var storage: UIViewController?
let selector: Selector
public var wrappedValue: T {
get { value }
set {
value = newValue
//invalidateBody()
}
}
public init(wrappedValue: T, selector: Selector) {
self.value = wrappedValue
self.selector = selector
}
private static func invalidateBody(of viewController: UIViewController) {
// if viewController.responds(to: selector) {
// viewController.perform(selector)
// }
}
public static subscript<EnclosingSelf: UIViewController>(
_enclosingInstance observed: EnclosingSelf,
wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, T>,
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, InvalidateBody<T>>,
selector selectorKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Selector>
) -> T {
get {
observed[keyPath: storageKeyPath].value
}
set {
print("subscript")
observed[keyPath: storageKeyPath].value = newValue
observed[keyPath: selectorKeyPath]
invalidateBody(of: observed)
//observed[keyPath: storageKeyPath].viewController = observed
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment