Washington Apple Pi

A Community of Apple iPad, iPhone and Mac Users

First Sips of Cocoa

By Dick Rucker

Washington Apple Pi Journal, reprint information

I have an itch that needs scratching. I want to learn how to build useful programs using Cocoa. For a non-programmer, that may be a naïve ambition, yes? Read on and see what you think. You might develop an inch, too.

I’ve dabbled with programming languages before, but more from curiosity than from any real need or desire to put them to use. But now, in one of my other hobbies — amateur radio — there are two applications I would like to have running on my latest Mac laptop, rather than on my aging Dell laptop. If I apply myself to learning how to program using Cocoa, might I be able to produce useful and pleasing applications for myself and perhaps other ham radio operators who are also Macophiles? That’s the open question.

Recently, a message appeared on the Mac Programming board of the Pi’s TCS forums that got me to seriously thinking this way. While I haven’t gotten to Cocoa yet, I have begun to learn the basics of Objective-C, which is probably the best language to learn for a programmer new to Cocoa.

Why Cocoa?

Cocoa is summarized in one Apple document as “a collection of advanced, object-oriented, application programming interfaces (APIs), including some that have been developed specifically for Mac OS X.” OK, let’s take that on faith for now.

Bill Cheeseman in his book Cocoa Recipes for Mac OS answers the “Why Cocoa” question this way (I’m paraphrasing):

What makes me think I can do this?

My past programming experiences are pretty limited:

Mid-1980s: I took a course in Pascal offered by the Washington Apple Pi and taught by Bob Platt. In those days, having a portable Mac meant buying a padded carry-on bag into which one could pack a 128K Mac, mouse, keyboard, and power cable. Though Pascal was a great tutorial language, and Bob was an excellent instructor, and Lightspeed Pascal was an excellent programming environment for its day, I never did anything more than play with it.

Early 1990s: Bill Atkinson’s Hypertalk was all the rage, and Dan Winkler and Scot Kamins wrote the very popular Hypertalk 2.0: The Book. I worked through that book one summer and it helped me to understand some new concepts such as messages, message handlers, and “the message path” that every message initiated by the user or the program follows to find its appropriate handler.

This was long before I had ever heard of object-oriented programming. I have since come to believe that Bill Atkinson was remarkably prescient about that. However, I never did anything more than play with HyperCard.

Another decade has now begun. It’s time for me to get acquainted with more modern programming tools, especially now that I have two applications in mind to build and programming seems to becoming more fun than ever before.

With that as motivation, I saw this last July on the “Mac Programming” board of the Pi’s TCS:

FROM: Jon Thomason Mac Programming
This looks very promising.
http://www.macdevcenter.com/pub/a/mac/
2003/07/22/cocoa_series.html

C is for Cocoa by Seth Roby:

“ According to those other tutorials, to learn Objective-C you need to know C, and to learn C you need to wade through a sea of things that, quite frankly, won’t help you at all as you start out in your Cocoa experience. But if that’s the case, why do the tutorials require this as base knowledge?”

“ This tutorial is different: here, you will learn all the C you need to know to learn Cocoa, and ignore the rest: these lessons will leave out the parts of C that, while useful, are not necessary to know in everyday Cocoa programming.”

The first installment takes you from how to spell ‘C’, up through writing just a line or two that does very little on its own but shows the process. And if you buy my prescription at all, that’s two out of four ideas needed!

http://tcs.wap.org/topic?b=prog&top=30

If this series continues, I’m sure it’ll be one of the greats of its day.

With that encouragement, I decided to give it a try.

Since new ideas stick with me better when I can attach to them the names of real people and real events, here’s what I’ve dug up so far in trying to understand where C, Objective-C, and Cocoa came from. After this digression, we’ll close with a little more on Seth Roby’s tutorial.

A language named “C”

The Objective-C language is an extension of another language simply named “C.” Dennis Richie created C while he was working on the Multics project for Bell Labs. Multics was an early attempt at time-sharing expensive, and sometimes idle, mainframe-computing resources among a number of users at on-line terminals.

Project MAC at MIT was started at the suggestion of J.C.R. Licklider and resulted in the proposal to built Multics. (At this point, I could digress into Licklider’s seminal role in the creation of what is now known as the Internet, but I won’t. I also don’t know what “MAC” in the project’s name stood for.)

Bell Labs withdrew as a Multics contractor in 1969 to begin its own time-sharing system development. Dennis writes: “By 1969, Bell Labs management, and even the researchers, came to believe that the promises of Multics could be fulfilled only too late and too expensively. Even before GE’s 645 Multics machine was removed from the premises, an informal group, led primarily by Ken Thompson, had begun investigating alternatives.”

Why did Dennis name his language “C”? Because he based it on the “B” language developed by another Bell Labs staffer, Ken Thompson, for Multics. “B”, in turn, was a stripped-down version of the Bootstrap Combined Programming Language (BCPL) also used in Multics.

BCPL only allowed a programmer to write values that could be directly converted to binary values for storage in the computer’s data registers and memory devices. Ken Thompson’s B language was also “typeless” and was designed for another of his creations, the Unix operating system.

In 1969, Ken completed the first version of Unix on a DEC PDP-7 computer;
In 1970, he completed the B language, and;
in 1971, he recoded Unix to run on the new DEC PDP-11 computer.

Meanwhile, Dennis Richie was busy developing his C language. Since the new PDP-11 computer provided several different fundamental representations to work with:

one of his goals was to write a language that incorporated the notion of “data types.”

By 1972, Dennis had produced a useable version of C.

In 1973, Ken Thomson proceeded to re-write Unix in C for the DEC PDP-11.

There must be something very special about an operating system and a language that have so successfully evolved together over 30+ years. At least Apple Computer has bet its future on them through its commitment to Mac OS X. The many flavors of Windows seem to pale in significance. I’m thoroughly intrigued.

Why Objective-C?

To do this subject justice would require a dissertation on object-oriented programming (OOP) and this language’s place within it. Short of that, let me just say that my introduction to OOP was an eye-opening book: Brad Cox and Andrew Novabilsky, Object-oriented Programming, An Evolutionary Approach, Addison Wesley Publishing Company, Second Edition 1986.

Brad, who was the father of the original version of the Objective-C language, was driven then by a fundamental idea: that computer software was being designed and produced like handcrafts. That is, individual craftsmen doing their own things, perhaps very well, but with no means for combining their talents and products in a more industrialized manner. That meant that software development was a slow and expensive process and with only weak means to reuse anything in other products.

Brad thought software development would be better off copying the design and production methods of the makers of integrated circuit (IC) chips. He described in some detail how “software ICs” could be designed, produced, sold as parts, revised, and reused again to build bigger and better applications. He created the language Objective C to help fulfill his dream. Brad’s Objective C was an extension of the C language onto which he layered the object-oriented concepts of Alan Kay’s SmallTalk language.

Let’s turn to Bill Cheeseman’s book again and paraphrase a few key thoughts about Objective C as it exists today in the context of Apple’s Cocoa development environment:

  1. Objective-C is surprisingly easy to learn if you know C. (that’s a plus!)
  2. It has dynamic object-oriented extensions to standard C that make it possible to design applications in ways that are difficult or impossible using more traditional static programming languages such as C++. (that seems worth pursuing!)
  3. “Developers learning Objective-C often report that they have experienced a magical ‘Aha!’ moment, when their understanding of the language gels and wide new horizons of possibility suddenly become visible.” (OK, I’m sold. Are you yet?)

Seth Roby’s “C is for Cocoa”

Jon Thomason’s first TCS message summarized what’s in the first lesson. As subsequent lessons have been placed on-line, Jon or someone else has posted the updates to the Mac Programming board of the TCS:

FROM: Jon Thomason Mac Programming

>> The first installment takes you from how to spell 'C', up through
>> writing just a line or two that does very little on its own but shows
>> the process.

http://www.macdevcenter.com/pub/a/mac/
2003/07/22/cocoa_series.html

The second installment is online now, and shows how a series of lines can be grouped together to form a function: a unit of programming that you can reuse.

http://www.macdevcenter.com/pub/a/mac/
2003/08/01/cocoa_series.html

This series is shaping up nicely. Is anyone here reading it as an introduction to programming from scratch? I'd be interested in your impressions.

FROM: Jon Thomason Mac Programming

I'd missed the third installment. The fourth is up, and they're still superb.
The third installment introduces an important programming concept: the "flow" of control in a program. How one gets from here to there, one step at a time, including how to decide which of two ways to turn at a logical intersection.

http://www.macdevcenter.com/pub/a/mac/
2003/08/19/cocoa_series.html

The fourth installment shows how to do what computers do best: to keep doing the same thing over and over again unflinchingly, and to know when to stop.

http://www.macdevcenter.com/pub/a/mac/
2003/09/09/cocoa_series.html

I should print these out for my wife. She was always better than me at math, anyway. Who knows what she could accomplish with C... let alone Cocoa!

FROM: Richard Rucker Mac Programming
The fifth was posted today. It covers pointers to allocated memory spaces in the heap and points out that Cocoa makes extensive use of them.

http://www.macdevcenter.com/pub/a/mac/
2003/10/07/cocoa.html


I've been saving each build as a separate main.c file for later reference. So far, I have the following folders containing a corresponding main.c file:

main #7 - pointers
main #6 - for loop
main #5 - do…while loop
main #4 - while loop
main #3 - switch operator
main #2 - ternary operator
main #1 - basics

Each file is commented to remind me of the details of what was new at each step. At the bottom of each, I've included the "build and run results" in a final comment to show what that version does when run.

This is one of the best tutorials I've had the pleasure of working through.

The most recent lesson is entitled “The Double Life of Variables.” It introduces the concepts of “the Stack” and “the Heap” as the two places where C functions and their variables reside in our computers. It then shows how a programmer writing in Objective C refers to items in either place and updates their contents. By the end of the lesson, the student will have built and successfully run a program referred to as “main #7” in the list above.

My next step: to start working through Seth’s “C is for Cocoa” tutorials in Panther’s Xcode instead of Jaguar’s Project Builder. Want to join me?