Skip to main content

Use Xcode Breakpoint Actions to Replace NSLog Statements

·2 mins

Many Mac and iOS developers use NSLog or Swift print statements to log debugging information to Xcode’s console. You can eliminate these statements by using Xcode breakpoint actions.

Adding Breakpoint Actions #

The first step to creating breakpoint actions is to create a breakpoint. Click in the gutter next to a line of code in Xcode’s editor to set a breakpoint at that line of code. Right-click on the breakpoint arrow and choose Edit Breakpoint to open a popover like the following:

BreakpointEditor

Click the Add Action button to add a breakpoint action. Choose Log Message from the Action menu.

LogBreakpointAction

Entering Log Messages #

Enter the message you want to log in the text field. Most text you enter is logged literally. If you type the following message:

Breakpoint reached.

The text Breakpoint reached. is logged. In most cases when you’re logging, you want to log the value of a variable. To log the value of a variable, wrap it in @ characters, such as the following:

Length: @length@

If the value of the length variable is 10, the following message is logged:

Length: 10

Suppose you have a game where you have a player object that stores player data. The player has a vector value position, and you want to know the player’s x position. You would enter the following text in the text field:

Player X: @player.position.x@

If the value of x is 50, the following message is logged:

Player X: 50

Automatically Continuing #

When you looked at the screenshots, you may have noticed a checkbox to automatically continue after evaluating actions. In most cases you should select this checkbox. By selecting the checkbox Xcode logs the message and continues running your application. Deselect the checkbox if you want to step through your code after reaching the breakpoint.