recursiveDescription method in Swift?

2019-03-08 09:56发布

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?

8条回答
相关推荐>>
2楼-- · 2019-03-08 10:47

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)

查看更多
来,给爷笑一个
3楼-- · 2019-03-08 10:49

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
查看更多
登录 后发表回答