Finding Bugs with Breakpoints

The first step to using the debugger is to get into the mindset of using breakpoints. A breakpoint is a location in your code where the debugger will pause the app’s execution and let you inspect the app’s state. This gives you an opportunity to figure out what’s going on.

Consider the following function to calculate prime numbers up to a certain maximum, and return them as an array. It sets aside some trivial cases up-front with a guard and a switch, since negative numbers, 0, and 1 are not considered prime, and 2 is too simple a case to bother iterating over. In the default case, it takes each candidate value and looks for integers that divide evenly into it:

Get Xcode Treasures now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.