iPhone Development : Objective C Pt 2

iPhone DevelopmentIn the previous article I gave you a brief overview of Object Oriented programming and showed you a little Objective C code. Objective C is the language used to program on all the Apple device’s and in this article you’ll learn a lot more about it.

Software you Need

To develop iPhone app’s you will need to download some free software from Apple. You get it online here: http://developer.apple.com/iphone/. It’s called the iPhone SDK and the current version is 3.2 as of this writing.

While you can program in Objective C on PC’s, you need an Apple Macintosh to use this iPhone development software. You can get a Mac Mini for around $300 on Amazon. This was the type of Mac I first started developing on actually.

I trust that you have now bought a Mac and installed the software, or are going for the ride on a PC. Now, I’ll continue explaining how to program in Objective C.

Object’s in Objective C

I explained in the last article what goes into writing OOP program’s. Here I’ll show you the code involved. You define an object in Objective C by creating a class definition for it. Here is an example class definition, and main function that uses it:

#import <Foundation/Foundation.h>

@interface NewComputer: NSObject


int modelNumber;

int ram;


-(void) getModelNumber;

-(void) getRam;

-(void) setModelNumber;

-(void) setRam;


  • The first line of code, #import <Foundation/Foundation.h> imports all of the information in the file named Foundation.h into your program. So, all of the variable’s and method’s created in Foundation.h, are now available for your use.
  • Then we have the interface part of the class definition. @interface describes the basic variables and methods, that will need to be implemented.
  • The first line above is stating that this new object class is to be associated with the object named NSObject. Every object you ever create will at some point be derived from NSObject.
  • I then state that this class object will have to variables named modelNumber and ram. They both will contain integer value’s.
  • After the closing curly brace, I then define all of the function’s that need to be defined for this class. The use of the word void means that they will not be returning any value’s to user’s that call for these function’s.
  • After you have defined the basic structure of your class you close that part of the code out with the keyword @end.
  • Special Note: It is considered good form to start the name of your class with an uppercase letter. All other variable’s and method names normally begin with a lowercase letter. There is one exception, constants, which we have not talked about yet, are normally all cap’s.

@implementation NewComputer

-(void) getModelNumber


NSLog (@”The model number is %i”,modelNumber);


-(void) getRam


NSLog (@”The amount of ram is %i”,ram);


-(void) setModelNumber: (int) m


modelNumber = m;


-(void) setRam (int) m


ram = m;



In the implementation part of your class definition, is where you create the function’s that every object will have by default. You define the beginning of the implementation section with the line @implementation, followed by the name of the class.

It is also considered to be good form, to never directly allow the user’s of your object’s to be able to directly change or view the value’s for the variable’s of the class. We implement that rule by forcing the user to get and change those value’s through function’s. These function’s normally begin with the word’s get and set.

The first two function’s will print a message on the screen when called. NSLog is a function that was created and is stored in Foundation.h. Whatever value’s that are passed to it will be displayed on the screen, of whoever executes this program.

Every thing placed between @” and “, will be displayed. If you want to show variable value’s in this string of text, you tell the interpreter where those variable value’s will show up with the symbol %i. The values will appear based off of the order they are in after the quote “.

The other two function’s set the value’s for the two variable’s you defined for your class. The word void state’s that these function’s also do not return a value when they are called. Then I set the name of the function.

After the colon (:), you can see some odd code that looks like this (int) m. This states that this function can be passed an integer. If it is passed an integer, you want to assign that value to a variable named m.

You then will perform that function’s action’s inside of the curly braces { }. The code modelNumber = m; is stating that what ever value was passed to this function, should be assigned to the object’s variable named modelNumber.

//—Main Function—

int main (int argc, char *argv[])


NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NewComputer *yourComputer;

yourComputer = [NewComputer alloc];

yourComputer = [yourComputer init];

[yourComputer setModelNumber: 230];

[yourComputer setRam: 8];

[yourComputer getModelNumber];

[yourComputer getRam];

[pool drain];

return 0;


This is the main function. This is the part of the program that is run first by default, when the whole program is executed. I describe much of it in the previous Objective C tutorial. Here I’ll show you what is new:

  • NewComputer *yourComputer; : This line of code creates an object of class type NewComputer. Don’t worry about what the star does at this point. We’ll cover that later. This code is stating that you want a new computer created by the blueprint (class) you named NewComputer, and it should have the name yourComputer.
  • yourComputer = [NewComputer alloc]; : This states that you want the computer to provide you with storage space in memory for the yourComputer object.
  • yourComputer = [yourComputer init]; : This code returns to you the location of your new object.
  • Short Cut: You could actually combine these three line’s into one. If you did it would look like this: NewComputer *yourComputer = [[NewComputer alloc] init];
  • [yourComputer setModelNumber: 230]; : This code stores the value of 230 in the variable named setModelNumber
  • [yourComputer getModelNumber]; : This code calls for the method getModelNumber, to perform an action. It outputs the current value of the variable modelNumber to the screen.
  • [pool drain]; : Releases the memory your program was using back to the computer
  • return 0; : Tells the Objective C executor that the program ended successfully, without an error.
  • } : The final closing curly brace signals the end of the whole program.

That’s All Folks

Now you know how to create objects from classes in Objective C. This is one of the most complicated concepts you have to understand. In the next tutorial, I’ll talk about looping, decision making and data types. If you have any question’s, leave them below.

Here to Serve

Think Tank

2 Responses to “iPhone Development : Objective C Pt 2”

  1. Thanks for the tutorials, I’m really enjoying this obj C one so far. If anyone is going to try this example, please note on the implementation,

    ” -(void) setRam (int) m ” should be ” -(void) setRam: (int) m ”

    took me a while to work out why it wouldn’t compile!

    Thanks Ben

    • admin says:

      Thank you for pointing that out. I’m glad you like the tutorial. I actually was forced to take the Apple app part down. I’ll be extending this tutorial as soon as possible.

Leave a Reply

Your email address will not be published.