首页 文章

Xcode在调试时评估表达式

提问于
浏览
51

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

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

4 回答

  • 45

    没有回答关于Xcode的问题,但是JetBrains的AppCode以标准的IDE方式做到这一点,我们大多数人都知道从其他平台 .

  • 14

    我的做法:

    po [NSUserDefaults standardUserDefaults]

    显示: <NSUserDefaults: 0x6143040>

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

    显示: "CHF"

  • 77

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

    我意识到这是一个老线程,但它仍然是谷歌的热门话题,所以我认为值得回答 .

  • 4

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

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

    我还附上了下面的表达式命令的帮助信息 . 希望这可以帮助 .

    使用当前在范围内的用户定义变量和变量,在当前程序上下文中评估C / ObjC / C表达式 . 此命令采用“原始”输入(无需引用内容) .

    语法:expression -

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

    -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.
    

    超时:如果可以静态评估表达式(没有运行代码),那么它将是 . 否则,默认情况下,表达式将在当前线程上以短暂超时运行:当前为0.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]
    

    重要说明:由于此命令采用“原始”输入,因此如果使用任何命令选项,则必须在命令选项的末尾和原始输入的开头之间使用“ - ” .

相关问题