recursiveDescription method in Swift?

前端 未结 8 901
天命终不由人
天命终不由人 2020-12-23 09:50

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 hav         


        
相关标签:
8条回答
  • 2020-12-23 10:14

    Building on @Rob Mayoff's answer:

    extension UIView {
    
       /**
        Wrapper for useful debugging description of view hierarchy
       */
       var recursiveDescription: NSString {
           return value(forKey: "recursiveDescription") as! NSString
       }
    
    }
    
    0 讨论(0)
  • 2020-12-23 10:19
    expression -l objc -O -- [`self.view` recursiveDescription]
    

    There is needed enter it in Objective-C format because UIKit is in Objective-C framework.

    Recursive description only exists for debugging purposes. It's not part of the public API and so isn't available to Swift

    And Swift is a strict language and doesn't allow you to call functions that haven't been strictly defined.

    Objective-C, it's a dynamic language so you can call functions like this.

    So what we need to do is to tell the debugger to evaluate this expression in an Objective-C syntax

    And the way to do that is to use expression with the option - l objc

    -O, tell the debugger that we also want the debug description the same as po would do and -- to indicate that there are no more options.

    We need to put put [self.view] view in back ticks.

    [`self.view`]
    

    Back ticks is like a preproccess step that says first, evaluate the contents of this in the current frame and insert the result, and then we can evaluate the rest.

    My answer is taken from WWDC 2018 Session 412 advanced debugging with Xcode and lldb.

    0 讨论(0)
  • 2020-12-23 10:29

    First add a category @interface without @implementation in your bridging header.

    @interface UIView (Debug)
    - (id)recursiveDescription;
    - (id)_autolayoutTrace;  // This one is even sweeter
    @end
    

    then in your console

    po self.recursiveDescription() as NSString
    po self._autolayoutTrace() as NSString
    

    The key here is as NSString not as String

    0 讨论(0)
  • 2020-12-23 10:30

    In swift 2.0 you can simply run:

    po view.performSelector("recursiveDescription")
    

    In (tested with iOS10 Beta3) swift 3.0 this is a bit more complex:

    po let s = view.perform("recursiveDescription"); print(s)

    0 讨论(0)
  • 2020-12-23 10:31

    In order to access private / undocumented Objective-C API (like the -recursiveDescription method on UIView) from Swift you can do the following:

    1. Create a new Objective-C category on the class the private method is defined in (e.g. UIView).
    2. Hit Yes if Xcode asks you about configuring an bridging header. (If you have already an bridging header in your project this step will be skipped).
    3. The implementation file (.m) of the category can be removed.
    4. Declare the private method in the category header:

      // UIView+Debugging.h
      
      @interface UIView (Debugging)
      - (id)recursiveDescription;
      @end
      

    Now you can set a breakpoint and print out the recursive description in LLDB:

    po view.recursiveDescription() as NSString
    
    0 讨论(0)
  • 2020-12-23 10:34
    po view.value(forKey: "recursiveDescription")!
    
    0 讨论(0)
提交回复
热议问题