32

recursiveDescription is very useful when debugging a hierarchy of views. View controller hierarchies are also very important, is there an equivalent for this?

jrturton
  • 118,105
  • 32
  • 252
  • 268

4 Answers4

45

To put the answer concisely, I use the command below in Xcode's debugger console to print the view controller hierarchy:

po [[[UIWindow keyWindow] rootViewController] _printHierarchy]

P.S. This works only on ios8 and above and is meant for debugging purposes only.

Link to the article that helped me discover this and many other brilliant debugging techniques is this

Edit 1: In Swift 2 you can print the hierarchy by:

UIApplication.sharedApplication().keyWindow?.rootViewController?.valueForKey("_‌​printHierarchy")

Edit 2: In Swift 3 you can print the hierarchy by:

UIApplication.shared.keyWindow?.rootViewController?.value(forKey: "_printHierarchy")
Blixt
  • 49,547
  • 13
  • 120
  • 153
jarora
  • 5,384
  • 2
  • 34
  • 46
18

Update - similar functionality is now available in Apple-supplied form as the _printHierarchy method, so you don't need this category any more.

There is now:

Github: Recursive description category for view controllers.

This adds a recursiveDescription method to UIViewController which prints out the view controller hierarchy. Excellent for checking if you are adding and removing your child view controllers properly.

The code is very simple, included here as well as the GitHub link above:

@implementation UIViewController (RecursiveDescription)

-(NSString*)recursiveDescription
{
    NSMutableString *description = [NSMutableString stringWithFormat:@"\n"];
    [self addDescriptionToString:description indentLevel:0];
    return description;
}

-(void)addDescriptionToString:(NSMutableString*)string indentLevel:(NSInteger)indentLevel
{
    NSString *padding = [@"" stringByPaddingToLength:indentLevel withString:@" " startingAtIndex:0];
    [string appendString:padding];
    [string appendFormat:@"%@, %@",[self debugDescription],NSStringFromCGRect(self.view.frame)];

    for (UIViewController *childController in self.childViewControllers)
    {
        [string appendFormat:@"\n%@>",padding];
        [childController addDescriptionToString:string indentLevel:indentLevel + 1];
    }
}

@end
jrturton
  • 118,105
  • 32
  • 252
  • 268
10

Fastest method (in lldb/Xcode debugger):

po [UIViewController _printHierarchy]
erikprice
  • 6,240
  • 3
  • 30
  • 40
0

_printHierarchy is not providing recursive info for sub view component of VC’s view.

Approach 1: Use lldb command to get complete view hierarchy.

po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]

Approach 2: Best way to get all info using “Debug View Hierarchy” button from XCode debugger.

enter image description here

kaushal
  • 1,553
  • 18
  • 25
  • Erm, that's nice, but the question specifically mentions recursiveDescription and that I'm looking for an equivalent for the view controller hierarchy. – jrturton Nov 06 '16 at 08:34