Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recursiveDescription method in Swift?

Is there a way to use [self.view recursiveDescription] in Swift? I am trying to use this method but I am getting the following error:

'UIView' does not have a member named 'recursiveDescription' 

Any suggestions?

like image 624
Vik Singh Avatar asked Sep 17 '14 21:09

Vik Singh


People also ask

What is NSLog in Swift?

NSLog : NSLog adds a timestamp and identifier to the output, whereas print will not; NSLog statements appear in both the device's console and debugger's console whereas print only appears in the debugger console.

How do you write log in Swift?

SwiftLog - A Logging API package for Swift The usage is really straightforward. First you have to import the Logging framework, then you create a logger and you use that logger instance to print out various log messages. import Logging let logger = Logger(label: "app-identifier") logger.info("Hello World!")

How do I print a value in Xcode?

Select a variable and click the Quick Look button to see a preview of the variable, click the Print Description button to print a description of the object in the console.


1 Answers

If you want to display the view hierarchy in lldb, you do not have to add any categories or bridging headers or anything like that. When debugging Objective-C code, one would generally use the following command at the (lldb) prompt:

po [[UIWindow keyWindow] recursiveDescription] 

If, though, you've paused in a Swift frame, lldb may expect a Swift expression. You can, though, explicitly tell expr (the po abbreviation is actually calling expression) which language the expression is in:

expr -l objc++ -O -- [[UIWindow keyWindow] recursiveDescription] 

The same patterns occur in iOS 8, when viewing the view controller hierarchy, using:

po [UIViewController _printHierarchy] 

or, in Swift frame:

expr -l objc++ -O -- [UIViewController _printHierarchy] 

In WWDC 2018 Advanced Debugging with Xcode, they suggest getting yourself away from this complicated expr syntax by defining an alias, poc, by creating a text file, ~/.lldbinit with the following line:

command alias poc expression -l objc -O -- 

Then you can do things like:

poc [UIViewController _printHierarchy] 

It's worth noting that Xcode 8 introduced the view debugger (click on view debug button), offering a more interactive way to analyze the view hierarchy, largely eliminating the need for the LLDB recursiveDescription of the view hierarchy. (This was discussed in WWDC 2016 video Visual Debugging with Xcode (which is no longer available). Admittedly, sometimes we end up having to fall back to the recursiveDescription technique shown above, but most of the time the view debugger makes this a far more natural, intuitive process.

And in Xcode 9, they've expanded this view debugger so it now includes the relevant view controllers, too:

enter image description here

like image 64
Rob Avatar answered Sep 23 '22 23:09

Rob