In the first part of this 2 part series on scope we talked about the scope of methods and how to declare local variables. Do you remember what a local variable is? Can you explain it to your neighbor? It is important that you understand the distinction between a local variable, and a class variable, which is what we are going to talk about now.
You remember that local variables are only accessible inside of the scope they are declared in, right? You can't access local variables that were declared in another method. This makes sense. But as we talked about before, there are definitely times that we need to have access to variables throughout the entire class, no matter what method we are in, this is precisely what a class variable does.
In order to understand how class variables work, we need to finally take a look at the ExampleViewController.h file. Look at the image below to understand the main parts of the ExampleViewController.h file, or the header file:
Ok, so you're probably noticing that your header file does not look like the image above. I realized after looking at the header file that it was very empty, so I went ahead and filled in a few things to give us something to work with. As before, I'll go through and talk about each part I have identified in the image above:
A - Similar to the implementation file, the header file also has a place for importing files. This is necessary for when you are referencing other files or classes and the compiler needs to know where to look for information about what you need.
B - This part of the interface declaration specifies what type of class you are working with. Note that whatever is to the right of the : in the interface declaration is the class type. In this case, our ExampleViewController class is of type UIViewController. That probably doesn't mean a lot yet, but it will later on.
C - This is where you are giving a name to your interface, or class. Note that this field should be the same name as the name of your .h and .m files.
D - This arrow is actually pointing to all of the variable references. It is within the interface declaration that we can declare our class variables, or variables that we can access from all over the class, no matter what method we are in. Notice that the convention is the exact same, we are just declaring variables inside of the header file. The header file will take care of the task of letting the implementation file know that there are some class variables, or global variables as they are commonly referred to, that are available for use.
E - This last arrow is also pointing to this group of method declarations. Just like variables, we need to declare the methods that we are planning on using in our implementation file. This area of the header file is where you make these method declarations.
That wraps up our overview of the basic structure of the header file. Now lets see these class variables in action! If you have declared the variables in the header file like I have done above (for now you don't need to worry about declaring any of the methods in the header file except for the first one thisIsJustATestMethod), you should be ready to start using them in our implementation file.
Lets go ahead and make some basic variable assignments in our viewDidLoad method with our newly declared class variables. How might you go about using a class variable? Do you need to declare anything? Nope. Take a look at the image below:
That's pretty easy, right? We don't need to declare anything or anything! We just reference the class variable by name, Xcode recognizes it as a class variable, turns it green conveniently, and you are ready to use it! Let me reiterate the fact that the thing that makes class variables really useful is that you can reference them anywhere in the class. As we go along, the importance of that statement will continue to sink in. But in order for us to move onto how we can set our class variables in different places in the class, there are a few more things we need to cover.
Stay tuned for our next post! Don't bail on me!
This is awesome! I am figuring it out slowly but surely!
ReplyDelete