recursiveDescription method in Swift?

Vik Singh picture Vik Singh · Sep 17, 2014 · Viewed 13.4k times · Source

Is there a way to use [self.view recursiveDescription] in Swift? I am trying to use this method but I am getting the following error:

'UIView' does not have a member named 'recursiveDescription'

Any suggestions?

Answer

Rob picture Rob · Feb 20, 2015

If you want to display the view hierarchy in lldb, you do not have to add any categories or bridging headers or anything like that. When debugging Objective-C code, one would generally use the following command at the (lldb) prompt:

po [[UIWindow keyWindow] recursiveDescription]

If, though, you've paused in a Swift frame, lldb may expect a Swift expression. You can, though, explicitly tell expr (the po abbreviation is actually calling expression) which language the expression is in:

expr -l objc++ -O -- [[UIWindow keyWindow] recursiveDescription]

The same patterns occur in iOS 8, when viewing the view controller hierarchy, using:

po [UIViewController _printHierarchy]

or, in Swift frame:

expr -l objc++ -O -- [UIViewController _printHierarchy]

In WWDC 2018 Advanced Debugging with Xcode, they suggest getting yourself away from this complicated expr syntax by defining an alias, poc, by creating a text file, ~/.lldbinit with the following line:

command alias poc expression -l objc -O --

Then you can do things like:

poc [UIViewController _printHierarchy]

It's worth noting that Xcode 8 introduced the view debugger (click on view debug button), offering a more interactive way to analyze the view hierarchy, largely eliminating the need for the LLDB recursiveDescription of the view hierarchy. (This was discussed in WWDC 2016 video Visual Debugging with Xcode (which is no longer available). Admittedly, sometimes we end up having to fall back to the recursiveDescription technique shown above, but most of the time the view debugger makes this a far more natural, intuitive process.

And in Xcode 9, they've expanded this view debugger so it now includes the relevant view controllers, too:

enter image description here