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

Using primitive variables

From: Programming for Non-Programmers: iOS 7

Video: Using primitive variables

Variables fit into two general categories. There are primitive variables and pointer variables. Primitive variables are simple data types, like numbers and true or false values. I'm working in viewController.m which you can access by single clicking on For writing code, I actually don't need any of this information on the right side So, now I'm going to scroll down inside of my code, and look at line 17.

Using primitive variables

Variables fit into two general categories. There are primitive variables and pointer variables. Primitive variables are simple data types, like numbers and true or false values. So that's what we'll look at here. We'll talk about pointer variables later on. I'm working in viewController.m which you can access by single clicking on it from the project navigator on the left side of your screen. For writing code, I actually don't need any of this information on the right side of the screen, so I'm going to hide that menu by clicking the top right button.

So, now I'm going to scroll down inside of my code, and look at line 17. There's a hyphen and it says void and viewDidLoad. That's the name of something called the function. We'll talk about functions in more detail later on, but the code that we're concerned about is inside of the curly braces following that function name. Inside of those curly braces, which in my code is on line 20, you'll find a comment. Remember that comments start with two forward slashes. These are lines of code that are ignored by the compiler.

That enables you to disable, or enable lines of code, or write notes to yourself. This example here, is a note. Lets go to the next line, by going to the end of that line, and pressing Return. Now we'll create an integer variable called score. Remember, an integer is a whole number, so it doesn't have a decimal point. You create an integer variable by typing the keyword int. Remember that that's short for integer, but that's how we declare an integer variable in code.

Int, space, score, that's the name of our variable. Then we set the value. Let's set it equal to ten and then a semicolon to end the statement. Just like a period in a sentence. So now you may have noticed that there is a yellow triangle with a white exclamation point in it. That's a warning. Xcode is telling you that you created a variable but you haven't done anything with it yet. That doesn't mean that you did anything wrong. That's just saying that you haven't used your variable. We'll look at using it in just a second. It would be great if we had some way to check to see if we created the variable correctly.

In programming, you often create things with your code. And you just want to do a quick check to see if you did the right thing. In other words, that the code that you wrote is working how you wanted it to work. One of the easiest ways to do that is to write a special message to yourself, in the output window. You can do that using a command called NSLog. Let's go to the next line and create an NSLog statement. Type NS, both capitalized, and then Log with a capital L.

As I type NSL, you'll see that the NSLog statement is highlighted in a code hinting window. So, I'm going to press Return to have Xcode complete that statement for me. Now, what's highlighted is something that says NSSstring, asterisk, format. Don't worry about what that means right now. We'll get into that later. In here, type an @ symbol, and two quotes. Don't forget to end this statement with a semicolon. So put that at the end of the line. As an example of what NSLog does, let's type some text inside the quotes.

I'm going to type just that, some text. I'm going to run the application so the simulator will launch. And then, in the output window, I should see, some text. Okay. Now, my screen is still blank in the simulator. But if you look in the output window at the bottom of your screen, mine's all the way on the right. You'll see the words, some text. So, that's how we can right a message to ourself in the output window and check to see if the code is working. Let's go back to our code. Now, this some text doesn't really provide that much use for us because what we really want to know is if we typed the score line of code correctly on line 21.

So, I'm going to replace some text, not the quotes, just some text, with score, space, is, space, and then I'm going to type a special character, which is percent, i, and that i is going to be lowercase. This is a special character in your code that represents a placeholder value. It enables us to replace that text with the value that's inside of a variable. For example, right after the close quote, I'm going to type a comma, and them I'm going to type score.

Once it's highlighted in the code hinting window, I'm going to press Return to have Xcode complete that for me. When the code runs, the value of the score variable, which I placed right here after the comma, will pop into the spot where the placeholder is. Let's look at that and see how it works. I'm going to click the Stop button before I run the application, and then I'll click the Run button again. So, now I'm going to see score is ten. So that placeholder got replaced with the value of the score variable which we set as ten.

So again, we can add an extra zero onto that ten. Run the code again, and then we should see score is 100. Now let's say we are making a game, and when you did something in the game, you got some points. In a game, you often change the score. So, for example, on the next line, after we declare the score variable above the NSLog statement, I'm going to type score equals 110. Don't forget to add the semicolon there.

So now, I can test the application. And I'll see that line 22 actually overrides what's in line 21. So, we declare the score variable equal to 100. And on the next line, since code runs from top to bottom, we update the value of score to be 110. This is actually not incredibly useful in a game, because in a game, we want to add a value to the score, no matter what the score currently is.

So, if you kill a bad guy in your game, and you get ten points, we want to take whatever the score value is, and add ten to it. So to do that, you're going to type something called a short hand operator. To do that, we're going to take the line of code where we set score equal to 110, and then I'm going to replace 110 with score plus 10. So, let's say, set the new value of score equal to whatever score is currently, plus 10.

So now, when I test this again, I'm going to see score equals 110. You'll see that in the output window. So now I can change that ten to a 20, for example, test again, and score will be 120. As an example of a way to write this shorthand, you can actually delete score and this plus and then write the plus right before the equal sign and this means the same thing as the line of code we had before. The plus equals operator is just a shorthand operator for saying, add a certain amount after the equal sign to whatever the value of the variable currently is.

So, I'll stop and run again, and then I see score is 120 one more time. Finally, sometimes you just want to add one to a variable's value, and the super shorthand operator for that is, of course, we could just do score plus equals one, but you could also do score plus plus. Now, before I test this, I want you to think about what you're going to see in the output window. Stop the application, click the Run button. And we see score is 101. So, that adds one to a value of a variable. So, remember, primitive variable values are numbers and true or false values.

In this movie, we looked at numbers. One example of a number is an integer which you create using the key word int. Using an NSLog statement, you can put the value of a variable inside of the output window. You just have to use the %i placeholder.

Show transcript

This video is part of

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

42 video lessons · 9424 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.

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.