Xcode Debugging with Breakpoints (for Beginners)
As a lifelong learner, it is understood that making errors is part of the learning process. In programming, these pesky things are known as 🐞’s (bugs). Being a thorough and efficient debugger will make me a better programmer and eventually allow me to write excellent code in the long run.
I thought it would be a good idea to do some research into beginner level debugging within Xcode and how to familiarize myself with the Breakpoint debugging tool and some of its capabilities.
So, what is a breakpoint?
A breakpoint is a debugging tool that allows you to pause the execution of your program up to a certain moment.
Why should I use a breakpoint?
Creating “pause” points in your code can help you investigate your code to see where bugs occur.
How do I create breakpoints?
Easy. Decide on where you want to pause the execution of your code and click in the left gutter to create a blue breakpoint.
Let’s take a look at the fabulousness we call the “Debugging Area.”
What’s going on in the screenshot above?
I created a function findSumOfEvens() which sums two integers if BOTH are even. I set a breakpoint on line 25, after I called my the findSumOfEvens(n1: 2, n2: 4) function on line 24. The function was paused AT line 25, which means the second call of findSumOfEvens(n1: 1, n2: 6) was not called YET.
I also annotated a few useful Xcode debugging buttons / areas. Note:
- Continue / Pause = (keyboard shortcut is ⌃ + ⌘ + Y)
And these execution control buttons:
- Step Over = (keyboard shortcut is F6)
- Step Into = (keyboard shortcut is F7)
- Step Out = (keyboard shortcut is F8)
I ran the project, and the console printed “The sum of your even numbers is 6.”
Next, I clicked on the “Step Over” button to execute the next line of code (line 25) — which is where I placed the breakpoint.
Above, you can see me “stepping over” the next line of code actually EXECUTED IT and now my console has printed “Both of your numbers need to be even. Try again.”
How do I get rid of a breakpoint?
Right click on the breakpoint to delete:
Wait, what’s that (lldb) message in your console about?
LLDB stands for “low level debugger” which provides the underlying debugging environment for developing for iOS applications. It can be used to find and eliminate issues in your Swift and Objective-C code.
How are breakpoints and the LLDB related?
You can type in shortcut commands into the LLDB to help you set breakpoints at certain lines of code, set breakpoints at many points in your code, list all your breakpoints, and disable breakpoints.
Above, you can see that you can set multiple breakpoints by entering a “selector” (method name) in LLDB. This is especially helpful if you have MANY files and don’t want to click in all of them to set breakpoints.
Above, you can also “list” all of your breakpoints in your project.
Above, you see the detailed list information printed to the console, which includes a “hit count”, which indicates how many times we’ve reached a certain breakpoint.
Above, I use “disable” to disable all of my breakpoints. If I wanted to disable only my 2nd breakpoint, I would have entered “breakpoint disable 2.”
Above, I set a breakpoint at a specific location in my code: my ViewController.swift file at line 16.
To recap, the LLDB breakpoint commands I used were:
- breakpoint set
- breakpoint list
- breakpoint disable
If my project crashes, shouldn’t I just click stop and end the execution of the app?
From what I’ve researched, STOPPING (◾) ️the app gets rid of valuable debugging information that Xcode is trying to give to us (the developer) to help us fix the problem. I’m going to start using breakpoints and the execution control buttons to better step through my code to see where the errors are occurring.
I challenge YOU to use a breakpoint in every Xcode project you work on this week.
Embrace the bugs as an opportunity for you to learn.
May the bugs be ever in your favor.