Tips & Tricks

Debugging with AppCode – what, where and how

In this post I’d like to cover some of the essentials regarding debugging with AppCode – how to add breakpoints and manage them, step through code, review variables, evaluate expressions and so on.

Let’s start with adding some line breakpoints – to set one just click in the left gutter next to the line in question, you’ll see a red dot appear there.
To remove it click on it again. When you have many breakpoints in different parts of your project, you can easily manage them using Breakpoints dialog that you can invoke with Shift+Cmd+F8 shortcut or from the main menu Run | View Breakpoints. Here you can find all the breakpoints you have in your project and modify their settings, for instance make one breakpoint dependant on another. In the screenshot below the breakpoint at line 25 of AppDelegate.m will be disabled unless the breakpoint at line 14 in ListViewController is hit.

You can also add a boolean condition for a breakpoint, so that debugger will stop on it only when the condition is met. Note, that you can use code completion here.

Another useful option here is called “Log evaluated expression”. Check it to have an expression entered in the field below evaluated in the context of a breakpoint and its value displayed in the debugging console.

In the same dialog you can delete a bunch of no longer needed breakpoints, and also temporarily mute some of them, if you think you might need them later. To do so, just uncheck them in the tree. They will be ignored until enabled again. You can also mute/unmute a breakpoint from the editor by Alt+clicking on it in the left gutter.

Let’s start debugging. By now you should know how to create and run a run configuration. To debug, the shortcut is almost the same, except where you normally press R (e.g. Ctrl+R) you need to hit D (Ctrl+D). If you run your app by clicking the green arrow in the upper right corner of the IDE, hit another icon right next to you – the one with, right, a bug.

Once you started debugging, you’ll see the Debug tool window appear in the bottom of the screen, just like with the Runner.

Whether there was an exception, a signal or a breakpoint was met, the debugger stops and AppCode navigates you to the line of code where the problem or a breakpoint occurred. It also switches from Console to Debugger tab in the Debugger tool window. What do we have there?

On the left side you see the threads like in the Debugger Navigator in Xcode, only in AppCode you choose a thread from a drop down, not by expanding a node in the tree.

Now, on the right side you see the variables. What do you normally do when you want to see the contents of CoreData object? I think it’s safe to assume that you use “print description”, right?
Well, you don’t need to do any of that in AppCode. Just expand the node in the tree and explore the values.

Stepping through code
In the Debugger tool window, you’ll see several options for that:

  • Step Over (F8): run until the next line in the current method, skipping the methods referenced at the current execution point (if any).
  • Step Into (F7): step into the method called at the current execution point.
  • Step Out (Shift+F8): step out of the current method, to the line executed right after it.
  • Run to Cursor (Alt+F9): resume the execution and pause when the execution point reaches the line at the current cursor location in the editor.

Note: Force Step Over (Alt+Shift+F8), Force Step Into (Alt+Shift+F7) and Force Run to Cursor (Alt+Cmd+F9) allow you to do similar actions but they ignore existing breakpoints on the way.

Need to evaluate an expression? Alt+F8 at your service. Type in an expression you want to evaluate (code completion works here), or first choose a variable in the Variables view and then press Alt+F8. Not many know though, that you can invoke it from the editor as well, just position the caret within an expression and hit Alt+F8 to have it evaluated.

Hope, you’ve learned a thing or two from this post. See more tips’n’tricks.