Learn Objective-C Part 3 – Introduction To Variables

Welcome to part 3 of my Objective-C tutorials for absolute beginners. If you haven’t already, I recommend starting from Part 1 to ensure you’re able to understand everything I talk about within this tutorial. Anyhow, in this lesson, we’re going to work with something called variables.

Variables are essentially areas in a computers memory where data can be stored. For example, if I made a variable saying that my name is James, instead of having to type that out whenever I need to, I can just make a reference to that variable.

So what types of variables are there in Objective-C?

There are a few different variables available for you to work with in Objective-C, although in this lesson we are only going to be working with one type of variable called an integer variable. An integer is any whole number, or its opposite. Some examples of integers are 0, 63, -121, and 123456789. So how can we implement them into our program? It’s pretty easy.

Before we begin actually putting variables into our programs, there are some rules you should know. Variables can not start with capital letters, and can not contain spaces. If they do, your compiler will give you an error.

Some Examples Of Good Variables: myName, sum, playerScore

Some Examples of Bad variables: MYNAME, MyName, Sum, Player Score

Anyway, moving on. Let’s say I wanted my program to take a math problem and solve it. We could make a variable called sum and have that be the answer. Here is a program to do that below:

#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[]) {
 
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
int sum = 60 + 27;
NSLog(@"The sum of 60 and 27 is %i", sum);
[pool drain];
return 0;
}

If done correctly, your console should output something like this:

Screen shot 2013-05-12 at 5.46.22 PM

 

As you can see, we defined the variable first. This must be done for all variables before they can be used within a program or application. ┬áThe code “int” declares that the variable “sum” is an integer, or in other words, a whole number. Then we called the variable to be outputted using the code %i, and after the quotation, we put a comma and then the name of the variable.

We can actually make the other numbers themselves variables, and call to multiple variables as well within one line. But what about the sum? We’re going to change that up a bit too, in fact, we’re actually going to use the sum variable to add the other two variables. This is demonstrated below.

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
int numberOne = 33;
int numberTwo = 266;
int sum = numberOne + numberTwo;
NSLog(@"The sum of %i and %i is %i", numberOne, numberTwo, sum);
[pool drain];
return 0;
}

The result should look something like this:

Xcode Console

When displaying multiple variables within one line of code, you separate them out with commas, and they must be in order.

After reading this lesson, and applying what I’ve taught you yourself, you should now be comfortable declaring and using integer variables within your applications. Feel free to play around for a while and see what kind of stuff you can make using integer variables. We have only stepped our toes into the water, there are so much more that can be done with variables that we will learn in future lessons. For now, we are just sticking to the basics.

Want to learn how to make thousands of dollars of passive income in the App Store?

My friend Mike over at App Dev Secrets just released his new, fool-proof course that teaches you how to make amazing iPhone or iPad applications and games! Best of all, you need zero programming knowledge whatoever to get started!

If you've ever dreamed of having your own apps and games make thousands every month, head on over to Mike's website and check it out!

Speak Your Mind

*