Swift Components Tour – Breakpoints Step Over

In this lesson

Back in the Swift Components Tour app, let’s talk about how to use your breakpoints to step through your code.

Kyle Roberts
Swift Guru at Large

Kyle's Series

Transcript

Tap on time to skip ahead

00:10

Hello world, Kyle here with Brax.tv. I just wanted to check a little bit of logic that I have going on in the TextFieldViewController about animating that waiting label at the top. You can see here in the code I’ve set a few breakpoints at different parts of the method of animate label. We have one when the method is called, just so I know that it’s been called at every interval of the timer and the timer is set with an interval of one second. I have another breakpoint in a double nested if statement just so I can see if there’s a rare situation that this line of code is run. So, that when we do hit this breakpoint I know that we have hit that rare situation, that it has passed through two if statements just so we know what that situation is. Then I have another breakpoint near the bottom of the method that is still within this loop here but it does not fit into this first if statement.

01:03

Let’s just open this view controller and see when we hit our first breakpoint. Right away, we hit this first one. We know that the timer down here has passed it’s interval of one second and has called the selector on self.animateLabel(). This method has been hit. How do we continue what’s going on here? With this continue program execution play/pause button down here. I’ll press that and one second later we’re back at this same exact breakpoint. We didn’t hit either of our other two breakpoints.

01:36

So I’m wondering why at this point. Is there anything we can do other than just continue through the code this time? There actually is. These next three buttons after the continue program execution play/pause button are exactly that. Those buttons are called step over, step into, step out of. The usefulness of these is probably best show just walking through it with you.

02:02

I’m going to press this step over button. Do we know what’s going to happen? No. But I’m going to press it and it looks like we’ve just advanced to the next line. We’ve run the code that creating the new var of i = 0. We can see down here that in this debug area of the current scope at our breakpoint that we’re paused at, we can see that there’s a new int created that is set to 0. Let’s just step over a few more times and see if we hit this breakpoint this time since the situation is different. The text of the waiting label should be equal to the second element of the array that says waiting with one period.

02:42

Let’s continue stepping over to see what happens. We’ve arrived into this loop here, into this for loop, and are about to check if self.statusLabelText equals the current waiting string. We know that the status labels text is waiting with one period and the current waiting string in this for loop is waiting with no period. Let’s step over and it looks like we do hit this last breakpoint this time. All we’re doing is incrementing i by 1.

03:12

If we step over, we’re not just going to step out of the method, we’re going to actually continue back up to the top of this for loop. We’re at the top of the for loop, we advance one more time and the new waiting string is actually the second element in this waiting strings array, which is waiting with one period. We do know that that’s what the text equals in the status label at the top in the view controller.

03:38

We’re at this if statement again. As I step over we should actually be able to go into this if statement because the yes- the status label’s text does equal the current waiting string. Let’s step over. We’re into that if statement. What we’re doing here is we’re incrementing i, yet again. Now i should = 2 after that line is hit. We’re at another if statement that we’re not going to pass because it’s asking if i = waitingString.count, which the current count of the waiting strings count is 4. We’re not there yet. We step over. We continue past that if statement. We’re setting the status label’s text to the waiting strings element at i and i is now 2, so 0, 1, 2. The waiting string after this line is run should have two periods in it. We’ll step over, now we’re at this break. We’re going to break out of the loop and now at the end of the method. We can continue program execution so that the app will run again. We saw for just a second the waiting string does now have two periods.

04:44

I’m just going to press play and we should hit this, I’m just going to continue program execution and we should hit this third breakpoint twice. As you see as we step over, continuing through this loop progression, we’re going to set the status label’s text to the final element this waiting array. We are not going to hit this middle breakpoint yet because i will not equal 4 yet. It’s currently equal to three.

05:13

We’ll continue through that, we’ll break out of the method and continue program execution. Waiting should have three periods now, which it does. We’re back, we’ve hit this method again. Let’s continue program execution three times because we are going to hit this third breakpoint three times. I’m just going to do that. Now we can see that we have finally hit this middle breakpoint. That’s because in the loop of this array we are on a waiting string with three periods and that is equal to the current status label’s text. Now we have incremented i so that it is up to 4. I does in fact equal the waiting strings array.count, which is also 4. We’re going to reset i to 0 and then continuing past that we’ll break out of that if statement and then set the status label’s text to the first element in the waiting string, which is an index of 0. It will just say waiting with no periods. We’re going to continue program execution, open the app, and we can see that there are no periods. Fabulous. Thanks for watching.

Additional Info

Register to get access to additional resources and info.