Skip to content

Instantly share code, notes, and snippets.

@sukhrobkhakimov
Created June 14, 2022 08:14
Show Gist options
  • Save sukhrobkhakimov/2cde08523923d6b63b342b903890e44a to your computer and use it in GitHub Desktop.
Save sukhrobkhakimov/2cde08523923d6b63b342b903890e44a to your computer and use it in GitHub Desktop.
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