Ok, we have finally made it to our method calling debug series. This is going to be a treat. We just talked in depth about methods. Let's do a quick recap on what we learned. Do you recall what the purpose of a method is? What are the main two parts to a method signature?
We use methods in programming to go execute a block of code in another location that has a specific purpose. The two parts to a simple method as we described were the return type, that specifies the type of variable that is coming back from a method call (if any), and then the method name, which you use to actually call the method.
Let's dig into some method calling. Go ahead and make sure that your header file looks like this: (also, in case you are being offended that I am using images for my code snippets, I am doing it for two reasons: 1) It will most likely be the same as what you will see when you type it up on your screen (as far as the color scheme goes), and 2) When it comes to trying to familiarize yourself with conventions and best practices, there really is no substitute for typing out the code yourself)
And then now we are going to go ahead and provide implementations for our methods in the implementation file of our class, or .m file:
Do you remember what these { }'s are doing? They are defining the scope of these methods. We dont have anything inside of any of these methods right now, but make sure that your code has these methods included (note, there are more methods that should be in the class, I am just showing the ones we are going to be working with right now).
So before we get going we need to figure out a starting point. We have all of these methods that we would like to call, where can we stat making method calls? One of the prime places for achieving this purpose is in the viewDidLoad method. Remember that that is of the of first places that gets run after our view is presented on the screen, so it makes for a great starting point.
To begin, I would like to make a method call to our first method, thisIsJustATestMethod. Do you remember how to make a method call? What the syntax is? Think really hard. Ok, this is how we do it. We put down a [ to begin the operation, then we indicate what we are calling the method in. What are we calling it in? We are calling it on the ExampleViewController class, and since we are already in that class, we use self. So after we have [self then what do we do? We tell self what method we would like it to call and then close the brackets. Our method call is going to look like this:
So if we build and run our project, what happens? Nothing. That is to be expected, right? We haven't really setup any of our methods to do anything, other than to make a call. Let's go ahead add some code into our viewDidLoad and thisIsJustATestMethod to make things more interesting:
You see that we are making use of our globalString class variable as well as a new variable called tempString. How do class variables differ from local variables again? You can access class variables from everywhere in the class, whereas local variables are only accessible within the scope they are declared in.
So if we go ahead an run this bad boy, does anything happen? Nope. We are doing some things, but we aren't doing anything that would print anything out anywhere, or show us what is going on. I would now like to introduce one of the means that we can go in and make sure that the program is running the way we are anticipating. We are going to put some breakpoints in our app.
You will notice that in Xcode, between the file manager on the left, and the code editor in the center, there is a really thin white line area (not the really really thin one, just the pretty thin one). Try clicking around in there, do you see any blue arrows showing up? These are breakpoints! And they are your best friend. Breakpoints tell the compiler to stop and hand control back over to the user when it encounters a breakpoint. This allows us to get a really good idea of what is going on in our code. Let's go ahead and put in a few breakpoints:
Ok so I hope this is making sense. Before we run our program, we are simply telling Xcode that we don't want it to run through our code at a million mph, but we want it to stop at these certain points. We will tell it to continue step by step as we feel like it. So if you hit Run, this is the screen that you will see:
Whoa! There is quite a lot of information here, so just like before, I'll go through and explain it point by point.
A - This little toggle switch lets you know that debug mode is ON. If you deselect this option, you will notice that even though your breakpoints are still there, they turn gray, the inactive color. If you want to, you can start your app not in debug mode and enter in half way through, and you breakpoints will become active. This is just a convenient way for Xcode to let you know what you are doing.
B - Pretty straightforward, but these are your breakpoints. The blue color indicates that they are active, and that you are in debug mode.
C - This line, highlighted by green, indicates that this is the line your computer is at in the process of reading your code. You told it to stop when it gets there, and it has obediently listened to you. It is important to note that this line of code has NOT been executed yet.
D - We will talk about this more in later posts, but this window in the debug panel is called the console. This is where you can print different values and observe the behavior of your program as it runs. If that doesn't make sense, it will in a later post.
E - This window is called the Variables View. It is a quick and easy way to see the values of variables you have declared in your app as you are debugging it.
These next three buttons are an extremely important part of the debugging process.
F - This button is called the Step Into button. If you have a breakpoint on a line of code that makes a method call, you can choose this button to explicitly follow that call into the method it is calling, or else you can just choose to step over it and assume that there aren't any problems in that area.
G - The Step Over arrow is the button that you will use all of the time in debugging. Remember that when debugging, we are essentially telling our computer to drop out of warp speed, and talk to us at our level. This button is the way that we tell the computer to move onto the next line of code, one line at a time. So we stop at one breakpoint, and we want to start executing the code that follows after slowly to see what is going on, and where a potential problem might be. We use the Step Over button to do this one line at a time.
H - The Continue Program Execution button or Play button does just what you might think. It kicks the computer into warp drive again and the compier is off to the races again. The computer resumes reading your code as you have outlined it UNTIL it reaches another breakpoint (if you are still in debug mode).
Ok, with that explanation out of the way, do you feel like you have a pretty good handle on the tools we are dealing with? If you are with me still, you should be looking at an Xcode screen that is in debug mode, and that has an active breakpoint highlighted by the fact that control is waiting to read our first breakpoint's line of code. You with me?
Go ahead and hover the variable on the left side of the = sign, the globalString variable, do you see a yellow info label show up? What do you notice? It tells us that the variable type is NSString, that's fine. It tells us the name of our variable, that's cool. What about the next part? What does this 0x0 mean? Xcode is telling us that this variable doesn't have any information in it! It is blank and empty. But wait, aren't we assigning it a value on this line of code? Remember that when a line of code is highlighted by the debugger, it hasn't been run yet. That is important to remember!
Let's go ahead and execute this current line of code, and move ont the next line. How do we do that again? There is a button at the bottom that we need to use. Which one is it? We just want to move one line at a time, so go ahead and select the Step Over button.
Now that we have successfully executed our first line of code, let's g ahead and hover over our globalString variable again and see what changed. Instead of 0x0, we have something quite different don't we. Xcode is now telling us the location in memory that this variable is now living. That's pretty cool! Our tiny variable has it's own spot in memory to call home. So if we want to see the value of our variable, hover over the variable, and put your mouse pointer right over the name of the variable in the yellow square. You'll notice that it changes to a purple-ish color, and something new appeared on the left side of the box. Two little arrows pointing up and down show up when you hover inside of the info box. Go ahead and click on those arrows, and look at your first option: Print Description. This really useful feature does just what you might think, it prints the description (and value) of this variable to the console. To the what? The console. Remember that window at the bottom right of your screen? Scroll all the way to the bottom and you should see a new message that gives us the details we need about the value of our globalString variable in bold:

Is that the value yo got? It should be! This is one of the more useful ways of getting different variable values when debugging.
Now that we have moved onto our next line of code, we are in a bit of a dilemma. What are we doing on this line of code? We are calling a method, right? Well, we probably should go inside of the method to see what is going on there, so which button are we going to press in our debug panel? Step Over? Step Into? The answer is that both work in this case. I mentioned that Step Into is specifically for going into the method that we are calling. That is true, except if there are breakpoints in that method (like there is in this case), using the Step Over button will stop on the breakpoints in the method anyway. So go ahead and hit either of the buttons and let's make the jump into a new method!
The first line that we stop on is making a new local variable declaration. Step Over this line, and let's take a close look at what is happening in the second line in the method. If you hover over the tempString variable in the second line, what do you see? It actually gives you value right in the info box: This is thisIsJustATestMethod. What about if we hover over the globalString variable, does it still have it's value (you'll need to Print Description to see it in the console)? It sure does. So we make a new temporary variable, and we are going to write over the value that is already in our globalString variable. Hit Step Over and let's look at what happens.
If you print the description of the globalString variable now what do you get? This is thisIsJustATestMethod. That's pretty straight forward. Now that we are at the end of our method here, where are we going to go if we press step over? Think about it for a second. We have reached the end of the scope of this method that we called from viewDidLoad, where are we going to go now? Back to viewDidLoad! Go ahead and press the button and see how it works. We just ran through all of that code in the thisIsJustATestMethod method, and we only moved one line in viewDidLoad. Do you see how that works? I am going to belabor this point over and over so you will have ample opportunity to grasp what is going on.
Now that we are done with this basic method calling exercise, go ahead and deselect the debug mode (the breakpoints button at the top) and then press the play button.
This was the first of a few posts I want to make on method calling. I want to make sure that the concept behind it is crystal clear.
Stay tuned for my next post!