Leave a rating/review
Two bugs squashed, a few more to go! In this episode, you’ll learn how to step through code after pausing a breakpoint.
Until now, you’ve controlled where execution pauses by setting breakpoints. If you wanted to pause and then continue for a couple of lines, or even one line, you set another breakpoint.
This works, but you can start to build up a pile of breakpoints pretty quickly. The next thing you’ll is how to step through your code when it’s paused. This is such a powerful tool because it lets you execute your app one line at a time, inspecting the changes in state at each step.
Make sure no breakpoints are set and execution is stopped.We’ll add a temporary method in Task.swift in order to better demonstrate how to step through code.
Open Task.swift and add the following method in the Task structure:
func isCompleted() -> Bool {
return completed
}```
* Next, open **RowView.swift** and set a breakpoint on line **42**.
* Finally, change this line to the following:
```swift
if task.isCompleted() {
Run your app until execution stops at this breakpoint. There are three ways to step through your code.
The first way is called step into. This will execute the current line, but if there are any method calls, the debugger will step into those methods.
The line you’re on now, if task.isCompleted { includes a call to the newly-added isCompleted() method. Click the Step into button to step into the method.
You might be surprised to see execution pause up where the task property is declared. Swift is executing the default “getter” for the checked property.
Click the Step into button again and you’ll be back to line 42. Click the Step into button once more.
This time, you should be taken to Task.swift inside the isCompleted() method.
The next way of stepping through code is step out, which is precisely what you need next. Click the Step out button.
The debugger will do whatever else is needed in the isCompleted() method and will pause once again to where method returns.
Perfect, just where you need to be, back in line 42 in RowView.swift. The third way of stepping is called step over. Click the Step over button once.
This will execute the current line of code and pause at the next line. If the current line executes any methods, they will be stepped over.
Note, this doesn’t mean the code in the method will be skipped, it will be executed. It just means that the debugger won’t pause inside those methods.
You don’t need to see what’s happening in the hidden() method anymore. Click the Step over button and you should end up on line 45.
This is the next line of code to be executed given that isCompleted returned false, sending execution into the else statement. What do you think? You are now on your way to breakpoint mastery!
Change back the code in line 42 to read as follows:
if task.completed {
Now open Task.swift and remove the isCompleted() method. One important thing to note is that these changes you just made are not yet applied and showing in this run of your app.
The Continue button, as well as the other control flow buttons in the Debug area, work on the current debug session, picking up from where execution paused.
Build and run your app once again in order to stop the current debug session and run a new session from the beginning.
As you have probably noticed, when you’re already running and ask Xcode to run again, you’ll see a warning indicating that this action will stop the currently running instance and will launch a new instance. This warning helps remind you that you’re not resuming, and that instead you’re starting over.
Excellent, you now know how to fully controll the flow of execution, work with your breakpoints, and leverage knowledge for debugging.
In the next episode, you’ll learn about the call stack, and its role and importance. See ya then :)