16

I'm writing an iOS app and I need help using the built-in Xcode debugger. Suppose I have an object called HomeViewController that has three properties

@property (nonatomic) BOOL finished;
@property (nonatomic, strong) NSArray *myArray;
@property (nonatomic, strong) NSString *myName;

@synthesize finished = _finished, myArray = _myArray, myName = _myName;

Suppose I have a breakpoint in this class. How would I view the contents of these properties? I've tried things such as po myName, print myName and print [self myName] but I can't figure out how to do this. I've tried using LLDB but I keep getting the same error that this person encountered (lldb fails to print variable values with "error: reference to 'id' is ambiguous") . The accepted answer to this question was, LLDB is broken and that I should just use GDB; however, I refuse to accept that something so fundamental is broken.

Nevertheless, I've also tried using GDB with similar commands as above; however, I can't get GDB to work either. Help please

Community
  • 1
  • 1
Nosrettap
  • 10,940
  • 23
  • 85
  • 140

2 Answers2

29

Once you place a breakpoint, run, and the program stops at the breakpoint, hover your cursor over the variable/value you want to see like this:

enter image description here

You could also place an NSLog(@"%@", yourLabel.text); to view the contents of that label/other object type.

One other option is to run GDB in the console like this:

gdb
attach <your process name>

And then use the po (print-object) command to view the value of a variable like this:

po variableName

To view the value of primitive types (int, float, long, double, char, etc.), you can just use the print command while running GDB in the console like this:

print yourPrimitiveVariable

Hope this helps!

EDIT:

With the po command, you can print out the value of an object using both the property name (self.myProperty) or the ivar name (possibly _myProperty). I demonstrate this here:

enter image description here

pasawaya
  • 11,515
  • 7
  • 53
  • 92
  • So, if I use po do I have to print the ivar or is there a way to print using the property's name? (sometimes my properties have different names than their corresponding variable) – Nosrettap Jul 17 '12 at 00:36
  • @Nosrettap Try using the property name and if it doesn't work, I'll try to find a workaround. – pasawaya Jul 17 '12 at 02:31
  • @Nosrettap - I tested out the `po` and `print` commands and it looks like it works fine with both the property and ivar name. See my update... – pasawaya Jul 17 '12 at 04:56
  • 2
    Whenever I try to do this I keep getting the following error: `error: error: reference to 'id' is ambiguous note: candidate found by name lookup is 'id' note: candidate found by name lookup is 'id' error: 1 errors parsing expression` – Nosrettap Jul 17 '12 at 11:56
4

Try with following expression in debug area to print object,

p self.view.bounds.size.width

or use,

po self.view

p - Print is only uses to print normal/simple values while, po - Print Object works same as NSLog to print value of an object

Bhavik Modi
  • 1,517
  • 14
  • 29