recursiveDescription
is very useful when debugging a hierarchy of views. View controller hierarchies are also very important, is there an equivalent for this?
Overview. You use view controllers to manage your UIKit app's interface. A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed.
Specifically, a view controller manages a view hierarchy and the state information needed to keep those views up-to-date. Every UIKit app relies heavily on view controllers to present content, and you frequently define custom view controllers to manage your views and UI-related logic.
Go back to Xcode and click on the Debug View Hierarchy button in the Debug bar. Alternatively, go to Debug\View Debugging\Capture View Hierarchy.
The view renders presentation of the model in a particular format. The controller responds to the user input and performs interactions on the data model objects. The controller receives the input, optionally validates it and then passes the input to the model.
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")
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
Fastest method (in lldb/Xcode debugger):
po [UIViewController _printHierarchy]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With