Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print out a property's contents using Xcode debugger?

Tags:

xcode

ios

gdb

lldb

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

like image 714
Nosrettap Avatar asked Jul 16 '12 22:07

Nosrettap


People also ask

How do I print from Xcode?

In Swift with Xcode you can use either print() or NSLog() . print() just outputs your text. Nothing more. NSLog() additionally to your text outputs time and other useful info to debug console.

How do I print a variable in Xcode?

Print a variableClick the Add Action button and select Debugger Command. Put any print command in a debug console in a text field. Make sure you prefix it with po , e.g., po print(view) . Check Automatically continue after evaluating actions.

How do I debug in Xcode step by step?

When you run an application in Xcode, the debugger is automatically started and attached to the process of the application. Click the Run button in the top left or press Command + R. From the moment the application is up and running, we can start inspecting the process and, if necessary, debug it.

Does Xcode have a debugger?

The Xcode debugger provides several methods to step through your code and inspect variables. You can precisely control execution of your code from a breakpoint, stepping into and out of called functions as necessary to determine where your bug occurs.


1 Answers

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

like image 119
pasawaya Avatar answered Oct 17 '22 12:10

pasawaya