WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

How to debug your view hierarchy using recursiveDescription

Try this LLDB command to simplify your view debugging

Paul Hudson       @twostraws

Xcode has a built-in view debugger that captures all the views inside the currently running app, then shows them in 3D. You can then filter that list based on classes, subclasses, and contents of your views, or right-click on a view and choose Focus On UIView to view just part of your view hierarchy.

But while this view debugger is great to have around, it can be clumsy to use in two situations: trying to compare multiple view frames at the same time, and trying to compare one view’s frames across several runs of your app.

Fortunately, UIView has a hidden method called recursiveDescription() that prints out an ASCII rendering of your view hierarchy – all the views, their children, their positions, some of their content, whether they are responding to user input, and so on.

This method is specifically there for debugging purposes – it’s not something you would ever want to call in production – but because it uses plain text in Xcode’s console, it can be easier to compare one view’s position against another, or perhaps against itself if you’re looking at different runs of your app.

Swift doesn’t make it easy to call hidden methods; the easiest way I’ve found is to run this from the LLDB prompt in Xcode:

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

So, place a breakpoint in your code wherever you’d like to inspect your view hierarchy, and try it out!

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.3/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.