How to implement recursiveDescription method in Swift

You might find out that the private method of UIView recursiveDescription is useful, especially when you want to debug your view hierarchy. You could run the following command from the LLDB prompt in Xcode:

po view.value(forKey: "recursiveDescription")!

It would print the view hierarchy for you, all the views and subviews, and their positions and other properties.

So you might wonder how to implement recursiveDescription method in Swift?
You could use the DFS to traverse all the subviews recursively and print the description of each view like this:

func listSubviews(_ view: UIView) {
  for subview in view.subviews {
    print(subview.description)

    listSubviews(subview)
  }
}

Hope this helps,
Michael

DigitalOcean Referral Badge