recursiveDescription method in Swift? recursiveDescription method in Swift? ios ios

recursiveDescription method in Swift?


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


In swift 2.0 you can simply run:

po view.performSelector("recursiveDescription")

In (tested with iOS10 Beta3) swift 3.0 this is a bit more complex:

po let s = view.perform("recursiveDescription"); print(s)


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