ucantblamem

Introduction to Objective-c and Cocoa from a web developer’s perspective

2nd Sep 2007

Aren’t you a web developer?

Late last year I read a book called Advanced PHP Programming, which introduced me to the idea of learning other languages (from what I use at work) to gain better understanding of programming best-practices and paradigms.

Earlier in the year I did a big stint of Ruby and Rails which gave me great insight into the power of a truly objective language as well as MVC and ActiveRecord etc… For the last couple of months I’ve been studying Objective-c and the Cocoa API which, as you may or may not know, is the underlying language/framework behind Mac OS X.

So far I am completely blown away by how advanced it all is, I really expected it to be much more low-level and assembler-like, but I can safely say that Cocoa rivals the beauty of Rails and Objective-c is the perfect language to drive it. Unfortunately, to the untrained eye it can seem quite alien and the initial introduction is difficult if you’re coming from the web-based world.

So to help my coding brethren out, here is my little Introduction to Objective-c and Cocoa from a web developer’s perspective:

What the foo is Objective-c???

Wikipedia does a pretty good job of explaining the technical aspects of Objective-c, but in a nutshell it’s the “C” language with a solid implementation of Objects. And by that I mean REAL objects, not these tack-on PHP ones.

Objective-c on its’ own looks quite alien, but most sources say that most “C” programmers will have it figured out in about 2 hours and of course, being based on “C”, I highly recommend going and learning the basics behind “C” programming before launching into this - that’s what I did and it made the introduction relatively straight forward. It’s also worth noting that at the bottom of the “C language tutorial for Cocoa” there are links to learn about Memory management and Pointers & Arrays - these are also must have’s if you’re coming from the web-language arena.

So, quickly to recap the steps, ’cause they do have a particular order to them:

  1. Learn the basics behind C.
  2. Read up on Memory management.
  3. Get your head around Pointers & Arrays.
  4. Be introduced to Objective-c.

Deep breathe… Welcome to Application development!

If you’re still with me, congratulations on climbing the Mt Everest of learning curves. Seriously, it gets A LOT easier from here on in.

Hopefully by now, you’ve discovered that “C” has a lot of similarities to PHP (which isn’t surprising seeing as it’s built on C) and that for each (Objective-c) class there are two files, a header file (interface) and the actual class itself (implementation).

When I first started reading about interfaces in PHP5 I couldn’t for the life of me think of a practical use for them… But, while getting my head around Objective-c, the paradigm started to make sense; they give a quick definition of the class to the compiler so it can reserve memory etc… (I still don’t get interfaces in PHP, but anyway…)

And what exactly does this have to do with chocolate anyway???

Cocoa, if you haven’t guessed it already, is essentially the framework that most mac-applications are built upon (technically it’s an API, but calling it a framework sounds more appropriate). It is all based on NextStep or OpenStep as it is known now, which Apple took over before developing OS X. For that reason, all Cocoa core classes are prefixed with “NS” (NSObject, NSTextField, etc).

You can also write Mac applications in straight C; using the Carbon API, or C++, Objective-c++ and even Java if you want to, but Cocoa is Apple’s preferred method and the resources available for Cocoa far outweigh the alternatives.

Say hello to a couple of new friends

Both PHP and Ruby have extensive online documentation and resources such as tutorials etc… Unfortunately, Cocoa’s online documentation is sparse and the tutorials are scattered around the net.

It took me a while, but I found the perfect companions to help me get going with application development on the mac:

  1. Cocoa Programming For Mac OSX (book) by Aaron Hillegass.
  2. Cocoacast (Podcast) with Boris Klaydman and Russ Homsy
  3. A Mac with the Developer tools installed (i.e. Xcode, Interface-builder, etc…).

The book is THE BOOK for getting started with programming Macs and the podcast actually takes you through the book step by step, showing you practical solutions to problems and generally discussing (in more detail) each chapter. Seeing code in use like this and the little hints that Boris and Russ give along the way are invaluable!

So… like… are we done?

If you’ve followed me to this point, then you’ve made it over the great wall and you should be on the downhill run to developing world-class applications! :P

There is a lot to developing platform-based applications and there are still plenty of good resources out there, once you’ve made it to this point.

So, to answer your question: Yes; at this stage, we are done. Best of luck happy coders!

Leave a Reply