开发者

How do you watch or evaluate an expression in xcode like visual studio's immediate window?

In MS visual studio we just right click add开发者_如何学Go watch.

How does one do this in Xcode?


Use the po command in the Debug area

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

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

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

Example

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

po var1/var2

The Console will return something like:

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

Showing object properties

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

po [bankInfo city]

And it will return something like:

(id) $4 = 0x000069e8 Testville

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

po bankInfo.city

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


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

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

How do you watch or evaluate an expression in xcode like visual studio's immediate window?

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


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


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


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

How do you watch or evaluate an expression in xcode like visual studio's immediate window?

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


If you want to know when the variable changes, use 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 stopped at a breakpoint in viewDidLoad, and added a "watch" for total:

How do you watch or evaluate an expression in xcode like visual studio's immediate window?

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

Now, if I "continue" execution (

How do you watch or evaluate an expression in xcode like visual studio's immediate window?

), Xcode will pause whenever this property changes and I can see at what line of code total is changing, the stack trace to get to that point, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜