Skip to content

Instantly share code, notes, and snippets.

@vincent-pradeilles
Created March 11, 2023 13:58
Show Gist options
  • Select an option

  • Save vincent-pradeilles/e01dd68ef073fee26c4894ccffa708b3 to your computer and use it in GitHub Desktop.

Select an option

Save vincent-pradeilles/e01dd68ef073fee26c4894ccffa708b3 to your computer and use it in GitHub Desktop.
import Foundation
class A {
private var value: Int = 42
}
let a = A()
let mirror = Mirror(reflecting: a)
extension Mirror {
func firstChild<T>(named name: String) -> T? {
children.compactMap {
guard let value = $0.value as? T else { return nil }
return $0.label == name ? value : nil
}.first
}
}
let privatePropertyValue: Int = mirror.firstChild(named: "value")!
print(privatePropertyValue) // prints "42"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment