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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.