3

Suppose I select a UIView from the "debug view hierarchy" in the Xcode debugger. I can print its description; it looks something like this:

Printing description of $17:
<MyView: 0x7fc6a451c030; frame = (0 0; 375 270); layer = <CALayer: 0x608000029700>>

That's nice, but what I really want to do is call [myView myMethod] and print the result. In this particular case, myMethod returns an NSString, but I imagine that won't always be true.

I've read Calling methods from Xcode Debugger?, but it does not appear to help for my case, because myView is not self.

William Jockusch
  • 26,513
  • 49
  • 182
  • 323

1 Answers1

4
  • You can set the language to Swift in LLDB (the console)

    (lldb) settings set target.language swift
    
  • Add a breakPoint in the class where MyView belongs, and in a scope where you're sure that is it is already instantiated.

  • Launch the app, and when the breakpoint is reached:

  • type po (print object) followed by the name of your view

    (lldb) po $myView
    

if you want more details use p (short for print): (lldb) p myView

  • Call your method on the view preceded by e or expr(short for expression) :

    (lldb) expr $myView.myMethod()
    
  • To see changes in the UI call this

    (lldb) expr CATransaction.flush()
    

For more, here is the reference for lldb commands.


You could also use the memory address, given after printing the description of your UI element, and cast that to a usable type:

(lldb) expr -- import UIKit
(lldb) expr -- let $myView = unsafeBitCast(0x7fc6a451c030, to: MyView.self)
(lldb) expr $myView.myMethod()
//Or if you'd like to create a variable with that result:
(lldb) expr let $string = $myView.myMethod()
(lldb) po $string
ielyamani
  • 17,807
  • 10
  • 55
  • 90
  • Your answer implies that having the view as `$1` or the like is not enough to call a method on it. Is that correct? – William Jockusch Sep 30 '18 at 12:25
  • @WilliamJockusch If you print that object again the `$x` is going to change, so my belief is no, But, I've included in the answer a way to use the address given by the description. – ielyamani Oct 01 '18 at 19:19