QuickTip Series #2

Debug View Controllers Hierarchy

Hello guys,
No introduction needed! Let’s go straight for our quick tip number 2.

Goal

Debug the view controllers hierarchy in the console as sometimes the Xcode UI Debugger can’t give us what we are looking for

Tip

  1. Add a breakpoint where you want to debug the views and put the following code in the debugger console once you reach your breakpoint:
  2. Run the command in the lldb console

When debugging Objective-C code:

po [UIViewController _printHierarchy]

and for Swift:

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

Example

Below an output example of when using this command :

<PieOverlayMenu.PieOverlayMenu 0x7f86854755e0>, state: appeared, view: <UIView 0x7f868548c500>
   | <Pie_for_Drivers.SplitViewController 0x7f8686877800>, state: appeared, view: <UILayoutContainerView 0x7f8684ce8410>
   |    | <UINavigationController 0x7f8688896200>, state: appeared, view: <UILayoutContainerView 0x7f8685417d10>
   |    |    | <Pie_for_Drivers.LegsListViewController 0x7f8685604f40>, state: appeared, view: <UIView 0x7f8684d51290>
   |    | <Pie_for_Drivers.MapViewController 0x7f8685416e10>, state: appeared, view: <UIView 0x7f86851dab80>
   | <PieOverlayMenu.PieOverlayMenuContentViewController 0x7f8685476080>, state: appeared, view: <UIView 0x7f86855423c0>
   |    | <Pie_for_Drivers.MenuViewController 0x7f8685474320>, state: appeared, view: <UIView 0x7f86854845b0>

Under the hood

Apple added the debub view hierarchy feature in XCode 6. But to inspect the view controller hierarchy Apple only introduced this private helper : _printHierarchy silently in iOS 8.

Let me now explain what the expression does.
Basically, it’s just calling the private helper :P

However, the command is different for Swift and Objective-C simply because the methode _printHierarchy isn’t available in Swift.
So what we do here is that we tell LLDB to execute an Objective-C expression.

Let’s break down the command for Swift code :

  • po (for print object) is simply an alias to expr -O --
  • -l objc++ is here to specify the language used.



Voila!