Skip to content

Instantly share code, notes, and snippets.

@lucamegh
Created June 23, 2023 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucamegh/9c75468cd443f98096d0399803584ba1 to your computer and use it in GitHub Desktop.
Save lucamegh/9c75468cd443f98096d0399803584ba1 to your computer and use it in GitHub Desktop.
Lists the stored properties (both public and private) of the given instance.
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