Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using instance variables

From: Programming for Non-Programmers: iOS 7

Video: Using instance variables

In addition to the two main types of Scope identifies where you can use a variable. For example, we've been creating variables inside of the viewDidLoad function. So let's go create another variable under that comment.

Using instance variables

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.

Show transcript

This video is part of

Image for Programming for Non-Programmers: iOS 7
Programming for Non-Programmers: iOS 7

42 video lessons · 9590 viewers

Todd Perkins
Author

 
Expand all | Collapse all
  1. 5m 18s
    1. Welcome
      44s
    2. What you should know before starting this course
      1m 17s
    3. Related courses
      1m 8s
    4. Using the exercise files
      1m 19s
    5. Viewing the finished app
      50s
  2. 10m 24s
    1. Finding and installing Xcode
      35s
    2. Creating an Xcode project
      1m 55s
    3. Understanding the Xcode interface
      3m 54s
    4. Configuring Xcode for app development
      2m 14s
    5. Configuring the iOS Simulator for app development
      1m 46s
  3. 43m 43s
    1. Understanding how programming works
      2m 34s
    2. Understanding variables
      2m 56s
    3. Using primitive variables
      9m 10s
    4. Using pointer variables
      4m 51s
    5. Using instance variables
      5m 19s
    6. Connecting visual objects to variables
      8m 12s
    7. Placing a number variable in a string
      4m 33s
    8. Challenge: Create two variables
      54s
    9. Solution: Create two variables
      5m 14s
  4. 27m 14s
    1. Understanding functions, methods, and selectors
      4m 43s
    2. Using functions, methods, and selectors
      7m 1s
    3. Understanding parameter functions
      2m 10s
    4. Connecting a button to a function
      7m 47s
    5. Challenge: Create a counter app
      43s
    6. Solution: Create a counter app
      4m 50s
  5. 13m 38s
    1. Understanding conditional statements
      2m 35s
    2. Using conditional statements
      5m 21s
    3. Challenge: Build an on/off button app
      39s
    4. Solution: Build an on/off button app
      5m 3s
  6. 44m 59s
    1. Viewing the app's code structure
      2m 11s
    2. Setting up the user interface
      7m 9s
    3. Setting up variables and functions
      5m 8s
    4. Connecting all of the visual elements to code
      2m 59s
    5. Displaying tapped numbers in the calculator
      4m 47s
    6. Controlling when tapped numbers should not appear in the calculator
      3m 27s
    7. Making the Clear button clear all values
      1m 4s
    8. Setting the calculator to add or subtract
      4m 29s
    9. Showing the total when the equals button is tapped
      3m 40s
    10. Formatting a number with commas
      5m 25s
    11. Challenge: Add a multiplication button
      1m 5s
    12. Solution: Add a multiplication button
      3m 35s
  7. 24s
    1. Next steps
      24s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Programming for Non-Programmers: iOS 7.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.