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

- 118,105
- 32
- 252
- 268
4 Answers
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")
-
1In Swift: `UIApplication.sharedApplication().keyWindow?.rootViewController?.valueForKey("_printHierarchy")` – rob mayoff May 10 '16 at 23:46
-
Thanks. I will add it to the answer. – jarora May 11 '16 at 03:06
-
1And in Swift 5: `po UIApplication.shared.keyWindow?.rootViewController?.value(forKey: "_printHierarchy")` – pbodsk Nov 12 '19 at 11:57
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

- 118,105
- 32
- 252
- 268
Fastest method (in lldb/Xcode debugger):
po [UIViewController _printHierarchy]

- 6,240
- 3
- 30
- 40
_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.

- 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