Peter Steinberger

Pimping recursiveDescription

While working on PSPDFKit, more and more I embrace viewController containment to better distribute responsibilities between different view controllers. One thing that always annoyed me about that is that po [[UIWindow keyWindow] recursiveDescription] is less and less useful if you just see a big bunch of UIViews. I asked some engineers at WWDC if there’s something like recursiveDescription just for UIViewControllers, but they didn’t have a answer to that, so I finally wrote my own.

Regular output of po [[UIWindow keyWindow] recursiveDescription]:

Pimped output:

The solution is surprisingly simple. Add the following code somewhere to your project and you’re done. As a bonus, this also lists attached childViewControllers if you run iOS 5 or above, and it will show you if a childViewController has a greater frame than its parentViewController (this is usually an easy-to-miss bug).

Note the usage of __attribute__((constructor)). This will call the method at a very early stage on app loading (thus we need an explicit @autorelease pool):

This works with or without ARC, iOS 4.3 and above. (imp_implementationWithBlock requires iOS 4.3, unless you want to perform some dark magic.)

For those if you that are curious, I use a private API call (_viewDelegate), but that’s obfuscated (it would pass an AppStore review) and you really only should compile that function in debug mode anyway.