[ACCEPTED]-How do you watch or evaluate an expression in xcode like visual studio's immediate window?-debugging

Accepted answer
Score: 37

Use the po command in the Debug area

Set up a breakpoint on the relevant area 17 of code, then when the program stops at 16 the breakpoint, enter commands in the Console 15 in the Debug Area. The relevant command 14 is po (print object) followed by the expression you want 13 to evaluate.

If the Debug window is not visible 12 in XCode, you can show it via the top menu:

'View' -> 'Debug Area' -> 'Activate Console' (XCode v8.x)

Example

To 11 evaluate an expression like var1/var2 where var1 and 10 var2 are both doubles, enter the following in 9 the Console:

po var1/var2

The Console will return something 8 like:

(double) $2 = 3.085 [no Objective-C description available]

Showing object properties

You can also return a particular property 7 of an object currently used in the code 6 at that breakpoint:

po [bankInfo city]

And it will return something 5 like:

(id) $4 = 0x000069e8 Testville

Note though that the Console doesn't 4 seem to like the dot notation and prefers 3 the square brackets when applicable. For 2 example, this returns an error for me:

po bankInfo.city

I 1 hope this is what you've been looking for.

Score: 14

Gabe's answer is almost there but missing 9 one crucial detail: Select Debugger Output . By default 8 the bottom option is set to Target Output, so 7 the po command doesn't show you anything.

Here 6 is a sandwich app from a tutorial I'm debugging:

eval expression screenshot in xcode

Being 5 an xcode newbie and coming from a MS Visual 4 Studio Background, I wanted exactly what 3 the OP was looking for. While playing around 2 from reading Gabe's answer I selected Debugger Output and 1 got what I wanted.

Score: 2

My seniors told to use NSLog(@variable)..........

0

Score: 1

Set some breakpoints in the begginning of 12 the looping and functions. Once u click 11 on the breakpoint(one similar to arrow) button 10 in the editor window the "Build and debug 9 tool" will get enabled. You can then go 8 to the debugger by clicking the debugger 7 icon. on the right of the debugger window 6 variables will be visible select self->then 5 the instance variable u r going to set watch 4 point.Right click on that and select "watch 3 variable".A trigger point will be set and 2 you will be notified with the value of the 1 variable when changed.

Score: 1

As fas as i understand you would like to 7 see when a variable is changing. For this 6 make a breakpoint and right click on it 5 and choose Edit Breakpoint. A window will appear:

enter image description here

Make sure 4 you choose the right action like Debugger Command or Log Message and check 3 the tick down at the options Automatically continue after evaluating. So you get 2 some kind of action (e.g. logging, sound, etc) and 1 wont stop at the breakpoint.

Score: 1

If you want to know when the variable changes, use 8 a "watch":

  1. Set a breakpoint somewhere in the class in question;
  2. Run the app in the debugger and let it stop at your breakpoint; and
  3. Go to the "Variables" view in the left side of the bottom "Debug" panel and right click on the property in question and choose "Watch".

For example, here, I've 7 stopped at a breakpoint in viewDidLoad, and added a 6 "watch" for total:

Watch

(This is Swift, but 5 the same is true for Objective-C, too.)

Now, if 4 I "continue" execution (enter image description here), Xcode 3 will pause whenever this property changes 2 and I can see at what line of code total is changing, the 1 stack trace to get to that point, etc.

More Related questions