Objective-C on Mac OS X

a tutorial for real beginners

copyright 2001 by T. Gene Davis

Hello world!

So you want to learn to program. Better yet, you want to learn to program an Apple computer. Well you’re in the right place. You can get all kinds of cool development (“development” is just a fancy term for “programming”) tools free for your apple.

You too, can install for free Perl, Tcl, gcc(cc in this case), Project Builder and other tools. There are actually so many choices of how to learn to program for Apple computers, that the number of choices can be a stumbling block. Also, most of the tutorials already out there are not geared toward new Mac X users that don’t already know how to program. The key problem is that when you’re the knew guy on the block, you need more simple examples to help you than the Senior C++ Core Engineer with the PhD. would need, and PhD types don’t generally think slow enough for the rest of us to keep up.

So here you will find a set of tutorials to get you started, without scaring you (too much). This is not at this typing printed in a real book anywhere, so feel free to contact your favorite publisher and force them to publish tutorial : – ) .

In the beginning

Where to start is the BIG question. I won’t waist your time with enumerations of all your options with their pros and cons. Why bother, you’re trusting me enough to teach you, so let’s learn. Here are my two recommendations: Java and Objective-c. This tutorial will focus on Objective-c, ’cause it’s cool, fast, powerful and poorly documented (from the “newbie” perspective)

Objective-c is an object oriented verion of c like c++ except more object oriented and nowhere near as complex to learn. “WHAT?! What are all those wierd words?” you ask. If you’re not familiar with the terms “c”, “c++”(pronounced see-plus-plus) or “Object oriented”, don’t sweated it. When you’re in a conversation with “gurus” and they say these terms just smile, and nod politely with this knowledge: Most people who should know these topics, only think they do. And if you study hard, you too can gain their level of incompetence!

Objective-c is a language that can be used on just about any computer, but mostly it is used to program for Mac OS X. We will focus on OS X as the development enviroment in this tutorial. Creating your first program is a BIG hurdle. So we will spend some good quality time working on your first program.

A word of warning

Don’t give up. Also don’t think this will be easy. This tutorial will make a start possible, but it is a start. If it was easy to become a good programmer, do you really think that big companies would be willing to paying the best ones three to four times what the average joe makes? Really. Be prepared to take baby steps at first, and spend alot of time with the silicon god. You will feel that it is going too slow, that you’ll never learn this stuff, but it will come. Just keep trying.

Ready, … set, …

There are two ways to program Objective-c on OS X. One is using an IDE. IDEs are cool tools that in the true tradition of windowed enviroments, present you with all options you could ever want, but no clear view of how to get started. You won’t remember what IDE stands for, so just assume it mean Incredibly Difficult Enviroment. You’ll love IDEs when you get use to programming, but professional IDEs are no place to begin. Project Builder is the free OS X IDE. Plan on learning it — later.

Now, if you have a developer tools installation disk that came with your OS X disks, use it to install the development tools. Do a standard install. Don’t be afraid to install too much. I don’t think that you can. If you don’t have a a developer tools cd, then go to Apple’s website at: //www.apple.com. Navigate around to the developer section of the site. At the time of this writing, there is a section of the developer site that you need a password to get to. To get a password, you need to become a member of ADC (Apple Developer Connection). The cheapest version is free, so go ahead and register for free. Apple will let you choose a user name and password. Log in the there software download area and download a copy of the developer tools. NOTE: This is a huge download, and took me many hours over a 28.8 modem connection. If you have the option, download it in pieces. Or better yet, download it on your friend’s computer :-0

Follow the instructions given by Apple, and install the developer tools.

Writing your first program

Open a new Finder window. In the top of the window is an icon of an “A” made out of writing instruments. Click on it and it takes you to the folder, “/Applications”. Double click the TextEdit icon. Now, just so you understand, there are alot of programs like TextEdit. Most people call them word processors. Don’t ever call them that again. You’re a computer programmer (wannabe anyway), so you now call them text editors, or just editors. Project Builder has a great text editor designed just for programmers, but any editor (the simpler the better) will do.

Just remember, always save as plain text, and you will be fine with all editors.

Now type this in to your editor:

#import <Foundation/Foundation.h>

main() {
    NSLog( @"Hello World!" );

You’ve just written your first Objective-c program. Feel different? Perhaps taller?

If you’re serious about learning Objective-c, memorize this program. No I’m not kidding. You need to know how to type it from memory. Re-type it 100 times if that is what it takes. Just learn it.

When your finished, save one correctly typed version of the program as “HelloWorld.m” (not “HelloWorld.m.txt”) in your home directory. That should be your default directory it is located at /Users/[your short username]. If your name was John Doe, you might be saving your new program in /Users/jdoe

Now I know a few of you are wondering what it does and how it works. Aren’t we all 😉 Well, let’s make it work first, and then I’ll go into details in the next chapter.

Compiling (creating the real program)

Open a new Finder window. In the top of the window is an icon of an “A” made out of writing instruments. Click on it and it takes you to the folder, “/Applications”. Now find the folder called, “Utilities”. Open it. Scroll down to the bottom of the window and look for an icon of a computer monitor’s black screen. The application is called “Terminal”. Run it.

On a side note, every developer that I’ve ever heard complain about the old Mac OS had as their number one complaint, that it had no terminal. So if you hear anyone complain about Mac OS not having a Terminal (sometimes called a command line or shell), simply snicker and tell them they’re outdated dinosaurs and should consider a less rigourous carreer (say management or marketing).

With the Terminal open you’re ready to create your first program. But let’s start with a few handy shell commands.

ls – list the contents of the current directory (folder)

pwd – where in the computer am I?

cd – change directory

cc – create a computer program

How do we use these? Shucks. It’s not hard.

First type in “pwd” and press return. You should get a response that looks like “/Users/jdoe”, only with your short login name there instead of “jdoe”. This would mean that you are in your “home” directory. If your not in your home directory, type “cd” and press return. Typing “pwd” will now reveal that your in your home directory.

Here comes the cool parts. Type “ls” and press enter. That’s LS, only lowercase. You’ll see some familiar names print out, such as “Documents”, “Library” and “HelloWorld.m”.

You can create your first program now. Creating a computer program is called compiling. To create the new program, type:

cc -o hello HelloWorld.m -framework Foundation

If you typed in the program correctly, it will seem like nothing has happened. This is a good thing. If you didn’t type in the program correctly, … oh, boy are you in for it. Just kidding. You’ll get all kinds warnings or errors. At this point read them, or ignore them — it’s your choice. Go back to the editor that you’re using and make the program match exactly what you were supposed to type in. Then recompile it. When it compiles without problems, your ready to move on.

To run the program, type:


You’ll get a statement similar to:


Feb 05 15:58:17 hello[3098] Hello World!


The “Hello World” portion should look familiar. Yes that is the sentence you typed into the program between the quotes. Now do the exercises and you’ll be ready for the next chapter.


Below are your first set of exercises. You won’t remember what you’ve learned, if you don’t practice. Do all of these exercises.

1) Change the name of the program that you create from “hello” to “howdy” by compiling with this command:

cc -o howdy HelloWorld.m -framework Foundation

Run the ./howdy program to make sure it works.

2) Compile the program to be called your name. Type in the command from memory. Do this from memory changing the name to two other names. Run each of the three new programs to make sure they work.

3) Re-type your HelloWorld program from memory, only this time save it as “MyGreeting.m” in your home directory. Next compile it using the command:

cc -o mygreeting MyGreeting.m -framework Foundation

Run the program using the command “./mygreeting” to make sure it works.

4) Re-type your HelloWorld program from memory saving it each time with a new name ending with “.m”. Compile each program, then run each compiled program.

5) Re-type your HelloWorld program from memory replacing the “Hello World!” phrase with “Greetings, carbon based life forms. Take me to your CPU!” Compile it. Run it.

6) By now you’re probably tired of typing variations of:

NSLog( @"Hello World!" );


Don’t worry, we’re almost ready to move on to the next chapter. First, however, type in your hello world repeating the line with NSLog three times. It should look like this:

#import <Foundation/Foundation.h>

main() {
    NSLog( @"Hello World!" );
    NSLog( @"Hello World!" );
    NSLog( @"Hello World!" );

Compile and run it.

new terms:

code – Computer program. The Hello World program qualifies.

compile – Create a computer program from the human readable code (like Objective-c).

development – The act of programming the computer.

newbie – A beginner.