Tuesday, March 29, 2011

Conditional Statements

One of the most important things in any program is the control flow. Objective-C has a very standard method of doing this. The keywords "if" "then" and "else" are found in almost every programming language.

To demonstrate this feature, we will make further amendments to the HelloWorld project we've been working on. Let's suppose we want a way of testing if our Person object is old enough to vote (in the USA, that's 18). To do this, we'll add a method called "oldEnoughToVote". Add the following method prototype to the interface of the Person class (right below -(void) setWeight...):

-(void) oldEnoughToVote;

Next we need to write the code for this method in Person's implementation. Insert the following lines of code in between @implementation and @end.

1 -(void) oldEnoughToVote{
2 if(age>=18){
3 NSLog(@"I am old enough to vote!\n");
4 }
5 else {
6 NSLog(@"I am NOT old enough to vote!\n");
7 }
8 }

Line 1 is the name of the method.
Line 2 is a conditional statement. It only executes the code in Line 3 if the condition between the parentheses is true. In this case, the condition is that the person's age needs to be greater than or equal to 18.
Line 3 is a print statement to the console.
Line 5 is an "else" statement, which the program jumps to if the condition in Line 3 is not met.
Line 6 is an alternative print statement to the console.

We're almost done! Now lets test this method in our main program. Insert the following lines of code in the "main" routine. You can go ahead and delete any other code that's between the brackets from previous lessons. Your new main routine should look like this:

1 int main (int argc, const char * argv[]) {
2 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
4 Person * junior;
5 junior = [Person alloc];
6 junior = [junior init];
8 [junior setAge:16];
9 [junior oldEnoughToVote];
10 [junior setAge:23];
11 [junior oldEnoughToVote];
13 [junior release];
14 [pool drain];
15 return 0;
16 }

Line 8 sets junior's age at 16. Line 9 tests if he's old enough to vote, and prints the answer to the console.
Line 10 changes junior's age to 23. Line 11 tests again and prints the answer to the console.

Click build and run at the top of your Xcode window to see this program in action! Make sure your console window is open, you can click 'Console' under the 'Run' menu at the top of the screen, or press SHIFT-COMMAND-R.

As you can see, when the oldEnoughToVote is called the first time, junior is only 16 and the program informs the user that he cannot vote. When we change his age to 23, the program retests and prints a different statement.

Stay tuned for more lessons on control flow. I think we'll do loops next time...

Monday, March 28, 2011

Creating an Object

In this lesson, we'll be creating a new object of the Person class, which we wrote in the last two tutorials.
Make sure the interface and implementation sections of person are in HelloWorld.m, or wherever your main routine is.

Type the following into your main routine:

1 int main (int argc, const char * argv[]) {
2 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
4 Person * junior;
5 junior = [Person alloc];
6 junior = [junior init];
8 [junior setAge:23];
9 [junior setWeight:200];

10 [junior eat];
11 [junior release];
12 [pool drain];
13 return 0;
14 }

Line 4 creates a new Person object named junior.
Line 5 grabs memory for that object.
Line 6 initializes the object.
Line 8 calls the setAge method on junior, with a parameter of 23. Junior's age is now 23.
Line 9 calls the setWeight method on junior, with a parameter of 200. Junior's weight is now 200.
Line 10 calls junior's eat method, raising junior's weight to 201.
Line 11 releases the memory allocated for junior.

Friday, March 25, 2011


Time to write some code for the person class!
Here is the implementation for our person class. Examine the following code:

1  @implementation Person
2  -(void) setAge: (int) a{
3     age = a;
4  }
5  -(void) setWeight: (int) w{
6     weight = w;
7  }
8  -(void) eat{
9     weight++;
10 }
11 @end

Line 1 lets the compiler know that this is where the code for the Person implementation begins.

Line 2 is a repeat of the declaration we saw in the interface section.

Line 3 is the operation that takes place in setAge. The age of the person is set to be the age you pass into the function.

Lines 5-7 are exactly the same as the setAge code, except instead of age, the variable is weight.

Line 9 is the operation for our eat function. weight++ means that the value of weight will be increased by 1. When you eat, you gain weight!

Line 11 signifies the end of the Person implementation.

In the next tutorial, we'll get to use our Person class definition to create a new person object!

Thursday, March 24, 2011


Objective-C, as you might have guessed, is an object oriented programming language.
Those of you who haven't had experience working with objects, here's a summary:
An object is basically a package of information with associated operations.

Let's say that a person is an object. A person has an age, weight, height. A person eats and sleeps.
We have three variables: age, weight, height
We have two functions: eat, sleep
When a person performs an action, the attributes of that person may change. For instance, if a person eats, their weight will probably change.

Examine the following code:

1 @interface Person: NSObject{
2 int age;
3 int weight;
4 int height;
5 }
6 -(void) setAge: (int) a;
7 -(void) setWeight: (int) w;
8 -(void) eat;
9 @end

Line 1 has @interface, which means you're creating a new type of object. The name of the object type is "Person". NSObject what most Objective-C objects inherit from (complicated stuff, I won't go over it now).

Lines 2-4 are the declarations for the variables we discussed earlier. These are all integer (number) data types.

Lines 6-8 are function declarations. The (void) before each of them indicates that they do not have a return type (we'll cover this later). Set age and set weight both take an argument (int) a and (int) w. To set the age and weight of the person, you need to give the object a number. The integer you pass in will eventually become the age or weight (respectively) of the person.

Line 9 has @end which means that the interface block has ended.

The interface is similar to a header file in C and C++. Prototypes are set for functions and variables are declared, but no operations are coded yet. Stay tuned and we'll write the code for these prototypes.

Wednesday, March 23, 2011

Analyzing Apple's HelloWorld Program

HelloWorld.m contains the following:

1 #import <Foundation/Foundation.h>
3 int main (int argc, const char * argv[]) {
4     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
6     // insert code here...
7     NSLog(@&quot;Hello, World!&quot;);
8     [pool drain];
9     return 0;
10 }

Line 1 is an #import statement, which behaves just like it does in Java, or like #include in C/C++
Line 3 is main... which should look familiar from countless other languages
Line 4 creates a "pool" of memory so that your program can use it
Line 6 is a one line comment. Multiple line comments follow the /* text */ format, like most languages
Line 7 is a print statement. The @ makes this line a format string as opposed to a C string
Line 8 gives the borrowed memory back to the machine
Line 9 is the return statement for this program (used to pass errors to the operating system)

Project 1 - Hello, World!

To create a new project:

1. Start up Xcode.
2. On the left-hand column, click "Create a new Xcode project"
3. In the new window, choose Mac OS X Application from the left hand menu. Select "Console Application" and change Type to "Foundation".
4. Hit choose and name your project (mine is HelloWorld)

Apple has already written our first application for us!
To run it, press the "Build and Run" button at the top of the project window.
To see the result of the program, the console needs to be open. Open it with either CMD-SHIFT-R or clicking "Console" under the "Run" menu at the top of the screen.

This program demonstrates how Objective-C outputs data to the console. The key component of this program is a file called "HelloWorld.m" (or YourProjectName.m). Feel free to change the text within the parentheses on Line 7 of this file to change what displays in your console when the program runs.

I know, it's not very exciting, but we now know how to prepare an environment where we can explore the intricacies of this language. Stay tuned for more!


The first thing we need to do is install Xcode. This is the environment used by developers working in Objective-C to build their Mac OS X and iPhone applications.

The latest version is 4, which is free for developers, but there is a $99 charge for that status.
Alternatively, you can pay $4.99 on the iTunes store.
What I'm using is Xcode version 3, which is free and can be found at the bottom right of this website. You may need to sign up for a developer account, but there is no immediate charge for this:


Download Xcode and follow the installation instructions. Then we can begin learning Objective-C!