Scope is another term that is used in programming that has a lot of meanings in a lot of different places. The concept is very important for everything we are doing moving forward so we need to make sure there aren't any questions about what scope is.
When we talk about scope, we are often times talking about the scope of a method. So for convenience sake, let's go ahead and look over the image we were working with in the last post:
We talked before about how C1, C2, and C3 are all methods. Methods are used to execute a specified bit of code, and only that bit of code. Does that make sense? When we talk about the scope of a method, we talk about the space that will be executed when that method gets called.
The scope of a method is defined by a pair of matching { } braces. Look up at the picture above, can you see how that makes sense? With C2 for example, we have a method called viewDidLoad (which gets called when the view finishes loading everything up), and then all of the code that resides within the scope or matching { } braces will get executed. Because you are going in and out of so many different methods throughout the life cycle of your app, it is very important to understand the scope, or the boundaries of each method.
To further understand the importance of scope lets get in and write some actual code like we did back at the very beginning of this journey when we talked about declaring variables.
Inside the scope of the viewDidLoad method, lets go ahead and declare some variables. We want to make an integer named integer1 with a value of 10, a double named d1 with a value of 45.6, and a string named text with a value of iPhone.
Pop quiz! Can you remember enough about how to declare variables to make those declarations inside of the viewDidLoad method?
Give it a whirl!
Ok, time is up, this is what it should look like:
Even though we didn't go over this initially, it is important to see that you can declare variables with values. So you don't need to declare them, and then set values afterward (even though you can do that), you can just set it all up in one line.
Let's start thinking about what we have now. This method viewDidLoad is one that gets called by the system every time the view finishes loading. In other words, we have a convenient place to do some stuff whenever our view finishes loading. Do you understand the scope of the viewDidLoad method? What lines of code are going to be executed when that method gets called?
Answer: All of them! Every line of code that is within the opening and closing { } or curly braces will be run. Make sure that makes sense because it is very important.
Another very important aspect of scope is that it acts like a sandbox between different methods in your implementation file. To illustrate what that means, consider the following two methods:
Look closely at what is going on, can you see what the problem is? Think about it. Think some more. Any ideas? Ok we have a new method here that I descriptively named thisIsJustATestMethod. Inside of the scope of that method we are declaring three new variables and giving them different names than in the first method. That's perfectly fine and dandy. But what about the values we are assigning to these new variables, why is there a red line underneath them? We declared them and assigned them values up above in the viewDidLoad method didn't we?
The problem here is that the definitions of the variables that we are referencing in this new thisIsJustATestMethod is out of the scope! It doesn't have any idea what those variables mean or what their values are because they were declared out of the method's scope. These variables are referred to as local variables. Local to the limited scope of the method. Do you see what I mean? Let's see if you can think of something you could do to make it so that we aren't getting errors in the second method. Any ideas?
How about this:
As it is with so many things, in programming there are a lot of different ways to solve the same problem. We don't really need to declare those variables inside of viewDidLoad, right? We could just move them down so that whenever the second method gets called, that code gets executed. Do you see any red lines? Any errors? Nope. That is because our scope issue is gone. We are referencing variables that are declared in the same scope and life is good.
So you might be wondering, there has to be a way to access variables outside of the scope of a small method, right? You are right. In my next post we will talk about how to get access to variables at a class level, known as class variables. Stay tuned!
No comments:
Post a Comment