Better print debugging with Xcode breakpoints

Debugging

The most common debugging technique that we all know is Print debugging. A print method is one of the first methods that I know when I jump into a new language, console.log, println, echo, puts, and for iOS, print and NSLog. It can help determine the cause of an error by track the flow and state of data at certain points of a program.

Pros #

The strongest point of print debugging is its ease of use—no need to learn anything. Just puts print wherever you want to track, and that's it.

Cons #

One of a drawback is you need to do a lot of cleans up after the test. Print debugging is it mean to be temporary and usually removed when the bug is solved. Failing to do so might slow-down the run-time. You would forget to remove it from time to time, and you might come up with a solution like removing print on release configuration.

An example of a way to remove print in release configuration.

func print(_ items: Any...) {
#if DEBUG
Swift.print(items[0])
#endif
}

But hacking print like that is just sweeping unnecessary code under the carpet. It still sits there and pollutes your codebase.

Another problem with debugging with print is that you need to rebuild the app if you want to add a new printing point, which is quite a pain if your project takes a long time to compile.

A better way with breakpoint #

Today I'm going to show you what I think is a better replacement of print debugging, a breakpoint. It is quite obvious that using a debugging tool would look cleaner and less housekeeping. But the main reason you use print is the ease of use. Let's see how easy it is compared to our beloved print.

To simulate print debugging with breakpoint:

  1. Double-click on a line number where you would typically put your print method.
  1. You will see a breakpoint editor window like this.
  1. Click the Add Action button and select Log Message. Put any string that you want to print out in a debug console in a text field.
  2. Check Automatically continue after evaluating actions. This will prevent the debugger from stopping at this breakpoint.

When the breakpoint's line is reached, you will see the log message print in the console. In this case viewDidLoad().

As you can see, the steps required to do this is very minimal. I think it can compete with the easiness of print.

To print a variable:

  1. Double-click on a line number where you would normally put your print method.
  1. You will see a breakpoint editor window like this.
  1. Click the Add Action button and select Debugger Command. Put any print command in a debug console in a text field. Make sure you prefix it with po, e.g., po print(view).

  2. Check Automatically continue after evaluating actions. This will prevent the debugger from stopping at this breakpoint.

Combine multiple actions #

The process of print a variable and string are quite the same. You can even combine multiple actions by clicking a plus icon (+) next to the Action selector to add a new action.

An example of a breakpoint with two actions
An example of a breakpoint with two actions

Pros #

As you can see, using a breakpoint can replace what print can do with a lot more benefits.

  • No print command all over the place, so no clean up afterward. You just disable a breakpoint or remove it after finished.
Disable (click) or remove (drag out) after finish debugging
Disable (click) or remove (drag out) after finish debugging
  • You can quickly move it around.
Move it around by dragging
Move it around by dragging
  • The best part is you don't need to recompile the app when adding/edit/move/delete your breakpoints.
You can add a new breakpoint and get a print without recompile the app
You can add a new breakpoint and get a print without recompile the app

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Tweet Share

If you enjoy this article, you can subscribe to the weekly newsletter.

Every Friday, you’ll get a quick recap of all articles and tips posted on this site — entirely for free.

← Home