Give your simulator superpowers

RocketSim: An Essential Developer Tool
as recommended by Apple

Debugging breakpoints as a replacement for prints

Debugging breakpoints in Xcode allows you to replace prints and reuse debugging statements in future debugging sessions. By simply enabling and disabling needed breakpoints you can also get rid of debug levels as often defined in libraries like CocoaLumberjack.

  • Reuse breakpoints by enabling them when needed
  • Use breakpoints to replace print statements
  • Keep your code clean without debugging logs

 

Stay updated with the latest in Swift & SwiftUI

The 2nd largest newsletter in the Apple development community with 19,438 developers. Don't miss out – Join today:

You can always unsubscribe, no hard feelings.

Using debugging breakpoints

The most simple implementation of debugging breakpoints is by adding a print breakpoint instead of adding a print statement in our code. Instead of the following code:

print("Did add \(content.count) content items to bucket \(bucket.name)")

We create a breakpoint on the line where we want the print to be triggered. By right-clicking the breakpoint and selecting Edit breakpoint... we enter the breakpoint detail view.

The breakpoint menu
The breakpoint menu

In this view, you are able to define the behaviour of the breakpoint once it’s triggered.

Editing a breakpoint
Editing a breakpoint

This will log the following line in your console:

"Did add 2 content items to bucket Photos"

po stands for print object and can be used to print any reachable objects within the context of the breakpoint line. By ticking Automatically continue after evaluating actions you allow the debugger to continue without pausing once the breakpoint is hit.

More advanced debugging with breakpoints

You can do a lot more with debugging breakpoints. A great start is to check out the talk by Paul Hudson “How to debug like a pro” which is available here.