Sometimes when building an app, the compiler won't find all the bugs in your code and everything will seem to be working—when in fact, there's a hidden bug. This video shows how to find and squash those hidden bugs using the Xcode debugger.
- [Instructor] Now let's look at using break points and a debugger to find issues in your code. So I have this very simple application set up, there are two text fields and some division is going to be performed and placed in this invisible label right here. And I say invisible but its actually showing, it's just empty, it has not text inside of it. So a very simple user interface with a button that's going to say when I click this button show the result in the text field. So if your following along with the exercise files you can create your layout just like this, low quality puns are optional.
So let's head over to View Controller dot Swift and I have everything connected to my user interface, so there aren't any tricks in here, everything's connected as it's supposed to be connected so I have those two text fields, the labels connected and doMath is connected to my button and all that does is it takes the result label and it displays the result of our get result method, which returns a float inside of a string. So pretty straight forward and I'm not showing what get result does just yet but just know that that's what the goal is.
It's going to calculate the results of the division, place it inside that label. So let's run the app and see what we get. So when the app runs up, I'll type in some numbers in here, the first one I'll do ten, tab over to the second one and I'm going to put two and I'll hit the button and then we get inf. And if you're wondering where that comes from, it's short for infinity. It's clearly not doing 10/2 some kind of issue is happening in our code.
So let's stop the app and head over to X-code. And instead of going through our code line by line to look for the error we're going to use break points. And if you're not familiar with break points, break points allow you to pause your running application and check the values of variables and constants so you can see where there are any issues in your code and they're very useful for what we want to do here. So to add a break point, simply left click to the left of your line number in your code.
And it adds this little break point icon right here, this blue arrow and with that created, that's all we need to start debugging our code. So we run the application and it's going to be pretty normal until we hit that button because the break point is inside of that doMath method. So until we click the button, nothing special is going to happen. So again I'll do 10/2 and hit the button and that's going to pause our application and bring us into the debugger.
Now it maybe a little bit confusing because this is what you see sometimes when you have a crash in your application or there's some kind of major problem that breaks your app, you're going to be taken here. But in this case, we're intentionally pausing our application. So here we are stopped on this line, and when you think of a break point stopping, you want to pay attention to these greenish backgrounds that are highlighted that's showing you where you're looking in your code.
So we're stopping right here and this is the current area that we're looking at, this green highlighted area. Now when you look in the debug area on the left side. You may not have this showing right now, to show it you'll need to make sure that this area is blue. So this shows and hides the left area. For what we're doing right here we actually don't even need the right area of the debug menu. So I'm going to hide that by clicking that right button. When you're debugging, you can think of the debugger as pausing at the beginning of the line rather than at the end.
So the value for text has not yet been set. So what we want to do, is see what is wrong by jumping inside of this get result method. To go inside of a method, you're going to click this button, it's the down arrow. So if you look in the debug area, look in the debug bar, find, step into, that's the down arrow. And what we want, like I said earlier, to go inside of get results. So when we click that button one time, its going to first go inside of result label and show us that that's connected to this variable.
So when I click it again, that's going to take us back to where we were before and so now we've already gone through that so we're looking at about this area in our code and when we hit step into again, it actually steps into the get result method and that's exactly what we want. So we're going to go through this method line by line. And instead of using step into, we're going to use the step over, which is go to the next line. Instead of go inside any method calls on this line.
So here we have our string str its set equal to the string zero. And you can continue to click this step over button a few times to see what's happening in our code. What we're concerned with, is the return value. The reason why we're concerned with that is because that's the only place that something can be wrong in our code. So we have this result label being set so we know that there's something wrong with the float that's returned from get result because that's all we have.
We have a empty string except for the value that's coming from get result, which for some reason is giving us infinity. So let's scroll down, and when we get to this last line by pressing that button, so that's where you want to stop. Keep on pressing step over 'til you get to this line being highlighted with green. And what you want to do at this point is actually look inside of the debug area to see the values of num1 and num2. Now if you look in here you'll see that num1 is 10 and num2 is zero.
Another way to check values of variables or constants is by hovering over your code while you're debugging. So if I hover num1 it says 10 and I hover over num2 it says zero. Now num2 being zero is not because num2 is processed on this line. We actually gave it a value earlier in the code. So the way to debug this problem, is to find out why we're dividing by zero, that's why we're getting infinity. Cause when we divide by zero that's the number that the code returns.
So let's go up through our code and look at why num2 is zero. So if we look at line 36, the value of num2 says it's already zero and it's coming from t2 which is the string zero. So let's scroll up and find t2 and we look at t2 and its set equal to str which is the string zero that's set on line 26. If we look at t1, its set to the value coming from the text field. So we're already getting the left text field, storing it in t1 but instead of storing the right text field in t2, we're storing that string value on line 26.
And that is actually a mistake in the code. So what we want to do is fix the mistake and test it again. So we'll stop the app, go back to X code and in here we want to make this simple change of changing this value to tRight and it's that right text field dot text. So we found the source of the problem. Now we're seeing this warning, I'm going to comment out that line so we don't have the warning anymore. And now that we've solved the problem, we'd no longer need the break point. So I'm going to disable it by right clicking and in here you can choose disable break point or if you want to delete the break point you can delete it here.
So disable is just, keep it there but don't use it for now. There's also edit break point, which gives you some options for customizing your break point. Those options are beyond the scope of this course but I recommend looking up the documentation under debugging and break points if you want more information about working with break points. So again I'll flick away, right click the break point, disable it, it turns lighter, showing that it's disabled, you can run the application again and everything should work properly.
So the app is running, we do 10/2, hit the button and we get five. So using the debugger, you can add break points to your application, pause it while it's running and find problems in your code.