Taking a step back to learn Objective C

Objective C is the language used by Apple to create IOS apps, so if you want to create native apps, you will need to learn it.

You can go a long way in IOS development (and not just using Interface Builder) without having to learn much of Objective C. You can parse through the line noise, and with a little experimentation, find out how to modify strings and perform arithmetic, iterate through loops, etc. until you’ve got a working program. But that’s not fun and it leads to unnecessary errors and frustration.

So let’s take a step back and learn Objective C.

In my opinion, Objective-C is the worst language esthetically (and conceptually) I have ever seen. It has more punctuation than Perl and less consistency than PHP. It’s object oriented system is half baked but to be fair it was created when OOP was a fairly new concept and apart from a handful Smalltalk afficionados, not well understood by anyone. So much of the language constructs were named when OO terminology was not yet settled. But many of it maps near enough to familier concepts, so we can go from there.

Let’s get started.

I promised an adventure game based on Star Wars, but let’s start with something simpler. Forget about IOS and GUIs for a moment and create a console app.

Screen Shot 2013-01-16 at 9.52.13 AM

Here’s hello world in Objective C:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    NSLog(@"Hello, World!");
    return 0;
}

Objective C is (very) loosely descended from C, and *theoretically* valid C is valid Obj-c. In this example, at least it holds true.

main is still the entry point to an application. You still have argc and argv, the only change being const which comes from C++ and is easy enough to understand.

#import is like #include but guarantees something is included only once. You can still use “#include” (because it’s valid C) but don’t.

NSLog comes from Foundation. Foundation is the Objective C core library. NS stands for “NextStep” the old NeXT workstation that Objective C was developed for. It is a bit more than printf or cout. More like System.out.println() in Java or System.Console.WriteLine() in C#. NSLog takes a format string and variable number of arguments.

In order to “cast” a string literal — an array of chars — to an NSString, use an @ symbol in front of the string. @"like this".

And of course return your error code of zero from main.

Technically, there is a memory leak here. We never released the memory allocated for the NSString created implicitly when we sent the constant character string @”Hello, World!” to NSLog().

In practice, this memory will be freed when the program exits so it’s not a big deal in a hello world app that ends immediately, but your IOS apps (hopefully) will not.

So let’s rewrite this program to use ARC – the automatic reference counter.

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSLog(@"Hello, World!");
    }
    return 0;
}

The @autoreleasepool block puts all the references created within it under ARC – which means the compiler takes care of it.

If you are familiar with Objective C garbage collection (why are you reading this?) then you should be aware that using ARC means that you do not call retain, release, autorelease, or dealloc. See the IOS 5 release notes on ARC for more details.

Advertisements

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