In addition to the two main types of variables; which are primitives, like numbers and true or false values; and pointers, which are strings and more complicated data, variables also have something called a scope. Scope identifies where you can use a variable. For example, we've been creating variables inside of the viewDidLoad function. That's this function right here that starts on line 17 and ends on line 21.
All the function code is in curly braces, and we're going to be talking about functions in more detail later on. So let's go create another variable under that comment. So I'm creating a new line after line 20 inside of the function, before the close curly brace. And again, we'll create an integer called score, and we'll set it equal to 10. Now, if I want to use this variable somewhere else. Let's say I have a game and the player has a score, and I want to be able to manipulate the score from other parts of my code. In other words, outside of this function.
So let's see if I try to reference score inside of another function. I'm going to scroll down to the didReceiveMemoryWarning function, which, in my code, starts on line 24. Right below the comment in there, I'm going to type score++. Remember, ++ is the operator that increments a value by one. You'll see on the left when I type score++, that there is a red icon with a white exclamation point in it.
I'm going to click that to see what Xcode finds wrong with my statement. When I click that, on the right side of the screen, I see, use of undeclared identifier score. What that says is I'm trying to manipulate a variable that doesn't exist. The reason why you're seeing that error is because we declared score inside of the viewDidLoad. That is the only place that we can reference that value. In other words, the score variable is only known within the context of the viewDidLoad function.
Outside of that function, we can't reference the variable. So that creates a problem. In a game, you need to reference a score all throughout your code. So we need a way to be able to reference some variables outside the scope of a function. Let's look at how that works. I'm going to delete the lines of code that I wrote so far. And we're going to discuss something called an instance variable. An instance variable is a variable that's accessible all throughout a code file.
So let's click on viewController.h in the Project Navigator. Again, you want to single-click so you don't open a new window. On line 11, you'll see a line of code that says, @interface. Go to the end of that line, press Return to create a new line, and type an open curly brace and then a return. Xcode will automatically write a closed curly brace for you and indent the code. Note that indentations are not necessary, they just make it easier for you to read your code. So now, let's declare our score variable here, int score.
Now, when we declare a variable inside of a .h file, we don't set the value, we just type a semicolon. So int score and then a semicolon. What that does is it says create a variable called score that is accessible all throughout, what's called in programming, the ViewController class. Classes are a deeper concept of programming that are beyond the scope of this course. For information about that, be sure to check out Objective-C Essential Training or Foundations of Programming: Fundamentals.
So, let's click on ViewController.m now to return to that file. Now, anywhere in this file or class, we can reference this score variable. So, inside viewDidLoad, I'm going to type score equals 10. Notice I'm not typing int score equals 10, because I already created the score variable inside of ViewController.h. So, the variable has been declared. I'm just setting its value here.
So, score equals 10. And if I go inside of didReceiveMemoryWarning, I can type score++ and not get any kind of warning or error. So, an instance variable is a variable that's accessible all throughout a code file. The .h and .m code files are connected. The .h file is generally used to declare things. For example, in this case, a variable. Those things are accessible throughout the .m file.
So if you want to create an instanced variable that you can use all throughout your code, create the variable in the .h file.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92496 Viewers
82 Video lessons · 104138 Viewers
71 Video lessons · 75617 Viewers
56 Video lessons · 106988 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Your file was successfully uploaded.