Objective-C Tutorial 1: Objective-C – The Basics

The first tutorial of this series will discuss the basics of programming the ‘OOP‘ way using Objective-C as the platform. This will be a minimalist article that is aimed at those who want to start coding for the iPhone, much more than any other intended device or platform.

When coding for the iPhone, essentially files which are classes are composed of a header and main file (.h and .m extensions respectively). 

A sample code snippet:


#import int main (int argc, const char *argv[])

    NSLog (@"Hello World:");

    int i;

    for (i = 1; i <= 5; i++)


      NSLog (@"%dn", i);


    return (0);


Basic  Objective-C applications run through the main() method, and this code simply uses NSLog to output to the console various things. The for loop which is synonymous in other object oriented languages such as Java and C++, loops five times to output the value of that counter (i), using %d to indicate that its a digit we want to input. 

About the Terminology
– A class represents an object’s type, complete with properties and code to handle individual actions, normally named in Capitalised-format (i.e Car.m). 
– An object  contains the values of the class as well as a hidden pointer to itself. An example would be Car aCar. 
– A message is a method call sort of, sending a message or request to an object to do something, like [aCar drive], contained within square-brackets. The object receiving the message works out in its class the appropriate method to run (or none if nothing can be found appropriately).
– A method is a block of code reacting to a message call, invoking a logical grouped function, such as loadData, for instance. 

Structure of an Objective-C class
The first thing you need when defining a class, is to describe information about the class, which is done in an @interface braced block:

@interface Engine : NSObject
  EngineType  engType;
  EngineSize   engSize;
- (void) getSize: (EngineSize) engSize;
- (void) setSize: (EngineSize) engSize;
- (void) init;

One thing to point out here is that in the first line, NSObject is known in this language as the upmost class, and every class in one way or another inherits from NSObject (we will do a tutorial on Polymorphism and inheritance in the next tutorial). 

The rest of the code defined the objects (within the curly brackets) with the names engSize and engType instance variables. Below that, methods that can get called through messaging.

Playing Around with Messages
Objective-C when calling methods with arguments uses a strange notation, similar to:  

[getCarBrand setModel: @”Mercedes”
      color: kBlueColor];

The setModel and color are names of arguments (and part of the method name too), so in this language, we do explicitly name our arguments, and it also forms part of the name of the method [getCarBrand setModel color] . 

After the @interface section we have the @implementation section, which is the actual implementation of what we declared in the @interface:

@implementation Circle
- (void) setEngineName: (EngineString) e
  self.engineName = e;

- (void) callCarModel: ()   //no parameters

The next article will discuss Instantiating Objects, Polymorphism and Composition. Stay tuned!
This entry was posted in General and tagged . Bookmark the permalink.

One Response to Objective-C Tutorial 1: Objective-C – The Basics

  1. Doron Katz says:

    You guys can let me know if anything isnt clear, or missing and I can add it in here. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s