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

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

In MS visual studio we just right click add watch.

How does one do this in Xcode?

6条回答
  •  情深已故
    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.

提交回复
热议问题