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

前端 未结 6 897
滥情空心
滥情空心 2021-02-05 06:08

In MS visual studio we just right click add watch.

How does one do this in Xcode?

相关标签:
6条回答
  • 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:

    enter image description here

    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.

    0 讨论(0)
  • 2021-02-05 06:38

    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:

    eval expression screenshot in xcode

    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.

    0 讨论(0)
  • 2021-02-05 06:42

    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.

    0 讨论(0)
  • 2021-02-05 06:43

    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:

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

    Now, if I "continue" execution (), 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 讨论(0)
  • 2021-02-05 06:44

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

    0 讨论(0)
  • 2021-02-05 06:56

    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.

    0 讨论(0)
提交回复
热议问题