Xcode在调试时评估表达式

时间:2011-01-28 06:24:58

标签: objective-c xcode debugging gdb

我正在开发一款iPhone应用程序。我是一名全职Java开发人员,我习惯使用Eclipse,我可以在其中放置一个断点并停止该过程。然后,我可以输入我想要的任何表达式,Eclipse将使用该过程中该点的值对其进行评估。

有没有办法在Xcode中做到这一点?我希望能够在断点处停下来,然后输入一些代码来评估它。 gdb控制台会让我poprint-object),但它确实有限。有什么帮助吗?

4 个答案:

答案 0 :(得分:84)

在XCode 4.0中,这有点隐藏在GUI中。当你在断点处时,你可能会在Debug区域内看到Variables View;它是显示局部变量等的窗格。右键单击Variables View并选择“Add Expression ...”

我意识到这是一个老话题,但它仍然是谷歌的热门话题,所以我觉得值得回答。

答案 1 :(得分:47)

我的做法:

po [NSUserDefaults standardUserDefaults]

显示:< NSUserDefaults:0x6143040>

po [[NSUserDefaults standardUserDefaults] stringForKey:@"Currency"]

显示:“CHF”

答案 2 :(得分:14)

在调试器中使用“expression”命令。使用它相对简单。只需键入命令表达式,然后按Enter键。然后会提示您输入表达式。这是一个例子

(lldb) expression
Enter expressions, then terminate with an empty line to evaluate:
2+2

(int) $2 = 4

我还附上了下面的表达式命令的帮助信息。希望这会有所帮助。

使用user评估当前程序上下文中的C / ObjC / C ++表达式    定义当前范围内的变量和变量。这个命令需要    '原始'输入(无需引用内容)。

语法:expression -

命令选项用法:   表达式[-f] [-G] [-a] [-d] [-t] [-u] -   表达式[-o] [-a] [-d] [-t] [-u] -   表达

   -G <gdb-format> ( --gdb-format <gdb-format> )
        Specify a format using a GDB format specifier string.

   -a <boolean> ( --all-threads <boolean> )
        Should we run all threads if the execution doesn't complete on one
        thread.

   -d <boolean> ( --dynamic-value <boolean> )
        Upcast the value resulting from the expression to its dynamic type
        if available.

   -f <format> ( --format <format> )
        Specify a format to be used for display.

   -o ( --object-description )
        Print the object description of the value resulting from the
        expression.

   -t <unsigned-integer> ( --timeout <unsigned-integer> )
        Timeout value for running the expression.

   -u <boolean> ( --unwind-on-error <boolean> )
        Clean up program state if the expression causes a crash, breakpoint
        hit or signal.

超时:     如果可以静态地评估表达式(没有运行代码)那么它将是。     否则,默认情况下,表达式将在当前线程上以短暂超时运行:     目前.25秒。如果在该时间内没有返回,则评估将被中断     并在所有线程运行时恢复。您可以使用-a选项禁用所有重试     线程。您可以使用-t选项设置较短的超时。

用户定义的变量:     为方便起见,您可以定义自己的变量,或者在后续表达式中使用。     您可以像在C中定义变量一样定义它们。如果是第一个字符     您的用户定义变量是$,那么变量的值将来可用     表达式,否则它只在当前表达式中可用。

示例:

   expr my_struct->a = my_array[3] 
   expr -f bin -- (index * 8) + 5 
   expr unsigned int $foo = 5
   expr char c[] = "foo"; c[0]

重要说明:因为此命令采用“原始”输入,如果您使用任何输入    命令选项必须在命令选项结尾之间使用“ - ”    和原始输入的开始。

答案 3 :(得分:5)

没有回答有关Xcode的问题,但JetBrains的 AppCode 以大多数人从其他平台知道的标准IDE方式执行此操作。

相关问题