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