-
-
Save vincent-pradeilles/e01dd68ef073fee26c4894ccffa708b3 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
| 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