Created
July 7, 2024 20:52
-
-
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
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 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