First steps with Objective-C

I have been intrigued by Objective-C for some time now. It is a programming language that, while it’s been limited mostly to the development of Software for Mac OS X, it has found a new niche as the preferred programming language for the iPhone. So much so, in fact, that this language has seen a growth of over 900% throughout 2008.

Objective-C is a superset of C that adds Object Oriented Programming based on Smalltalk. Its syntax looks a bit strange at first to those of us who are used to working in languages derived from C++ such as Java, C#, and even Python (I’m counting Python because I believe several of its conventions are strongly tied to the design of C++, such as the dot notation).

Just like C++, in Objective-C classes are implemented in two files: one with the interface, with the extension .h and one with the implementation, with the extension .m. Objective-C and C++ code can also be mixed in the same implementation file, which must be denoted with the .mm extension.

There’s nothing like a short code example to really show the look-and-feel of the language. In the example, we’ll declare and implement a class named “MyClass” and we’ll use it from a simple main function.

First, we declare the interface in the MyClass.h file:

#import <Foundation/Foundation.h>
@interface MyClass : NSObject
{
    int x;
    int y;
}
-(void)printCoords;
-(void)initFromCoords:(int)nx y:(int)ny;
@end

This interface declares that the instances of this class shall have two variables of type int and two methods: printCoords, which will print to the terminal variables x and y, and initFromCoords that receives two integers as arguments and assigns them to x and y.

Next, we implement this interface in file MyClass.m:

#import "MyClass.h"

#import 

@implementation MyClass

-(void)printCoords
{
    printf("(%i,%i)\n", x, y);
}

-(void)initFromCoords:(int)nx y:(int)ny
{
    x = nx;
    y = ny;
}
@end

With this, we’ve completed the implementation of the “MyClass” interface. Note the inclusion of the C header stdio.h and the use of the printf function.

#import "MyClass.h"

int main()
{
    MyClass* myInstance = [MyClass alloc];
    [myInstance initFromCoords:1 y:2];
    [myInstance printCoords];
    return 0;
}

As you can see, here is where the syntax completely differs from the “C++-style” languages. In Objective-C messages are sent to objects using a bracket notation of the form: [instance message], instead of using the classic dot (or arrow) notation. The message can optionally include parameters, as shown in the invocation of the initFromCoords method.

To compile this example from the command line (in Mac OS X), we use the following command:

gcc -x objective-c MyClass.m main.m -Framework Foundation

Once compiled, we run the application and – if everything is fine – we should obtain the following result:

$ ./a.out
(1,2)

I hope this simple example helps illustrate some of the concepts behind the language. This is just the tip of the iceberg; you can find a friendly introduction to the topic on Apple’s developer website.