Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Using primitive variables


From:

Programming for Non-Programmers: iOS 7

with Todd Perkins

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. 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.
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

please wait ...
Watch the Online Video Course Programming for Non-Programmers: iOS 7
2h 25m Beginner Apr 10, 2014

Viewers: in countries Watching now:

iOS app development is actually simpler than you might think—even if you're not an experienced programmer. In this course, Todd Perkins bundles the most important concepts in iOS into a quick course, explaining the development process in a visual way that people of any background can understand. No programming experience required! At the end, you'll have a finished app and a basic understanding of Xcode, the toolset for developing iOS apps; building blocks like variables, functions, and conditional statements; and interface design. You can also figure out if an iOS learning path is right for you, without a lengthy time commitment.

If you find you'd like to learn more, see iOS App Development Essential Training, Foundations of Programming: Fundamentals, or any of the other programming courses in our library.

Topics include:
  • Installing Xcode
  • Creating an Xcode project
  • Configuring the iOS Simulator
  • Understanding variables
  • Connecting visual objects to variables
  • Understanding functions, methods, and selectors
  • Connecting a button to a function
  • Using conditional statements
  • Setting up the user interface
  • Connecting code elements to build an app
Subject:
Developer
Software:
iOS
Author:
Todd Perkins

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.

There are currently no FAQs about Programming for Non-Programmers: iOS 7.

 
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.

* Estimated file size

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

Upgrade to View Courses Offline

login

With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

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 ?

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:

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.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts.
lynda.com provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now
 

A trusted source for knowledge.

 

We provide training to more than 4 million people, and our members tell us that lynda.com helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

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.