In order to really know Cocoa you first need to get a good understanding of the language upon which it is built. So, with that in mind, we'll be going over the Objective-C language for the next several blog posts. We'll start out by looking at Objective-C as a separate entity from Cocoa, and Apple for that matter. In our first few posts we'll cover the inception and history of Objective-C, it's life outside of Cocoa, the basics of the language, and finally how to create a simple application using nothing more than the GNU Compiler Collection's version of Objective-C. In latter posts, we'll look into how the language is integrated into the Cocoa development environment.
A quick history lesson
Objective-C, like Bjarne Stroustrup's C++, is an object-oriented extension to the C programming language invented by Brad Cox. Unlike C++ however, which was based on the ideas of the Simula programming langauge, Objective-C took its inspiration from the works of Alan Kay and incorporated Smalltalk's message passing features into the language. A somewhat amusing tidbit is that both languages were designed around the same time period (the early 80's) and with the very same goals in mind, namely to make the creation of large, complex programs easier to manage through code reuse.
Though both languages set out to solve a similar problem, their philosophies for solving that problem couldn't have been more different. As a result, one went onto fame and fortune (cough, C++, cough) and the other languished a bit trying to find its niche. Many arguments can be made for why C++ is now one of the most popular programming languages and Objective-C is rarely used by anyone other than Cocoa developers, but you can bet that at least one of the reasons is the performance of C++ compared to Objective-C. Unlike C++, Objective-C has a runtime which allows for many of the dynamic features of the language, unfortunately those same features have also made Objective-C historically slower than C++. But as Moore's law keeps chugging along and making this problem less and less relevant, Objective-C has bounced back and has become the programming language of choice for today's Apple developer. Since the language was built with many of the ideas of today's dynamic scripting languages in mind (e.g., Python, Ruby, Groovy), programming in Objective-C is (at least in my opinion) much more pleasurable and painless than programming in C++. But, rather than go on all day about the differences between Objective-C and other languages and the reasons why I think Objective-C is superior in many ways, why don't I kickstart this tutorial and get you up to speed with Objective-C so that you can start forming your own opinions soon enough.
Your First Objective-C Program
Thanks to Kernighan and Ritchie, it is now virtually impossible to learn any programming language without first greeting the world around you. So, let's go ahead and get our hello world program out of the way. Here is your very first program in Objective-C:
Look familiar? I sure hope so, because aside from the #import line, this is essentially the Hello World sample that started it all. Incidentally, the #import line in the example above works just like the #include directive that you're used to from traditional C programming, with the small exception that it also makes sure that the file you're importing isn't included multiple times. So, using #import means you'll never have to write the following in your header files ever again:
Now that we've written our first Objective-C program and discussed the one and only difference between it and its pure C counterpart, why don't we compile it and run it to see the fruits of our labor. To compile the program above using the GCC, assuming that you put the code in a file named hello.m (the '.m' extension is typically used for Objective-C implementation files), the following line will compile our sample code into an executable named hello:
% gcc hello.m -o hello -l objc
Let's break down the line above to try and understand what we just did. First, we pass in the names of all the files that we wish to compile (.m files only since the #import directives will tell the compiler which headers to include), in this case we are passing in the hello.m file that we created above. Next, we specify the name of the resultant executable file with the -o filename portion of the compilation line. Finally, we need to let the linker know that we will need to link in the Objective-C runtime by including the -l objc argument.
If the compile command above worked properly you should see no output other than a new prompt, but doing an ls in your current directory should reveal a new file named hello. To run it, just type ./hello at the prompt and press Return and you should see Hello world printed to the screen.
If everything has worked as I've just described, congratulations, you've just written, compiled, and ran your very first Objective-C program and you're ready to start learning the meat of the language. In the next installment, we'll dig a little deeper into what makes Objective-C special as we'll be covering the basics of classes and objects.