Created
June 23, 2023 11:19
-
-
Save lucamegh/9c75468cd443f98096d0399803584ba1 to your computer and use it in GitHub Desktop.
Lists the stored properties (both public and private) of the given instance.
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
func listProperties(of subject: Any, maxDepth: Int? = nil) { | |
func listProperties(of subject: Any, maxDepth: Int?, indentationLevel: Int) { | |
if let maxDepth, maxDepth == 0 { | |
return | |
} else { | |
let mirror = Mirror(reflecting: subject) | |
let indentation = String(repeating: "\t", count: indentationLevel) | |
for case (let label, let value) in mirror.children { | |
print("\(indentation)▶︎ \(label ?? "<unknown label>"): \(type(of: value)) = \(value)") | |
listProperties( | |
of: value, | |
maxDepth: maxDepth.map { $0 - 1 }, | |
indentationLevel: indentationLevel + 1 | |
) | |
} | |
} | |
} | |
print("\(type(of: subject)):") | |
listProperties(of: subject, maxDepth: maxDepth, indentationLevel: 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment