Getting Objective, part 1: An introduction to Objective C


1 November 2011, by

This is the first in a series of posts that will – I hope – give a bit of insight into Objective C: what it’s like to develop in, how to write an app, and a few pitfalls to watch out for. This first one aims to give a basic overview of Objective C, and why we might want to use it in the first place.

Why develop in Objective C?

Objective C is the core language for developing applications for iOS and OSX – if you want to install something on any Apple products, then you’re going to need to write it in Objective C. Long gone are the days when that “if” would be met with snorts of derision: Apple’s growth in the home computer market is massively outpacing the rest of the industry, and – with all due respect to Android – they hold the lion’s share of the mobile application marketplace. A lion’s share that is projected to grow even more

Pie chart showing the distribution of application revenue across mobile platforms

When you factor in the media penetration on top of market share, any customer looking to dip a toe into mobile applications is – unless they have a very specific need – likely to start with iOS.

What’s different about Objective C?

There are a few things worth noting about Objective C.

  • It is a complete superset of C. Any C code can be included in an Objective C programme, and (if you want to) you can write an entire application in C, without having to interact with any Objective C functionality.
  • Much like Ruby, it is very strictly object-oriented. It doesn’t go quite as far as having nil be an object (which Ruby does), but almost anything else in your code can be treated as an object.
  • Again like Ruby, it’s based on message-passing, meaning that you can ask an object what it can do before trying to do it.

The main difference with Objective C isn’t so much how the language works – all my points above are similarities rather than differences, after all – as what else comes with it. Apple have been working with Objective C for a very long time, and almost all of their libraries are available to developers. Most notable of these are the UI elements – it is incredibly easy to get up and running with an application that looks good, and having that concern taken out of the equation is one thing that makes developing in Objective C particularly attractive.

Hello World

No language introduction would be complete without a “Hello World” example. The Apple developer documentation (which is very good indeed) has one here, which also covers the process of setting up a project and so on. This example is very similar, but will pop up an alert that greets the world rather than just writing it on the screen, and ask how the user is feeling.

#import "HelloWorldAppDelegate.h"

@implementation HelloWorldAppDelegate

- (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello World!"
                                              message:@"How are you today?"
                                              delegate:nil
                                              cancelButtonTitle:@"Fine thanks"
                                              otherButtonTitles:@"A little tired", nil];
    [alert autorelease];

    [window makeKeyAndVisible];
    [alert show];
    return YES;
}

- (void)dealloc {
    [window release];
    [super dealloc];
}

@end

And it looks just like this:
Screenshot of the Hello World application popup

Next time

I’ll explain why there are so many square brackets, ready for us to get into a more full-featured example.

next article in series

Tags: , , , , , , ,

Categories: Mobile, Technical

«
»

Leave a Reply

* Mandatory fields


+ two = 5

Submit Comment