Created
June 14, 2022 08:14
-
-
Save sukhrobkhakimov/2cde08523923d6b63b342b903890e44a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public protocol KeyValueObservable { | |
var keyPaths: [String: PartialKeyPath<Self>] { get } | |
} | |
extension KeyValueObservable { | |
private subscript(descendant key: String) -> Any { | |
Mirror(reflecting: self).descendant(key)! | |
} | |
public var keyPaths: [String: PartialKeyPath<Self>] { | |
var keyPaths = [String: PartialKeyPath<Self>]() | |
let mirror = Mirror(reflecting: self) | |
for case (let key?, _) in mirror.children { | |
keyPaths[key] = \Self.[descendant: key] as PartialKeyPath | |
} | |
return keyPaths | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment