Published on MacDevCenter (
 See this if you're having trouble printing code examples

Programming With Cocoa

Digging Deeper into Mac OS X


By now you have installed your fresh copy of Mac OS X on the shiny new hardware you bought in anticipation of its release. Exploration of Apple's revolutionary new operating system has begun in earnest. You spend hours poking and prodding -- searching to discover all Mac OS X has to offer. Your significant other is dead tired of seeing that damned genie effect for the 100th time. You sit smug knowing that you possess the legendary power of Unix fused with the elegance of a Mac.

Learning CocoaLearning Cocoa
By Apple Computer, Inc.
Table of Contents
Sample Chapter
Full Description
Read Online -- Safari

But now you're probably yearning to understand Mac OS X on a more fundamental level, closer to the nuts and bolts. You want to get your hands dirty and program on it, make it do the things you want it to do. Fortunately, Apple has thoughtfully included a copy of its developer's tools CD with every copy of Mac OS X. That's right, the same tools used to create Mac OS X and its applications are now in your hands. Apple has empowered users of Mac OS X to go a bit further with their exploration, to go a bit further and create their own applications. It's a good time to own a Mac.

In this article I want to convince you that even if you've had no prior application development experience (note: some programming experience is assumed) that you too can learn how to create full-fledged, functional, Mac OS X applications.

This is the beginning of a journey. Every couple of weeks, I will publish a column that shows you how to master object-oriented programming as it relates to the Cocoa API. Sound daunting? It's really not. Bear with me. First I'll tell you about my own experiences learning Cocoa. I'll help you get a feel for the journey ahead. Then, I'll explain how Cocoa fits into the overall Mac OS X picture. After that, in two weeks we roll up our sleeves and get to work.

But I'm getting ahead of myself. Let me tell you a little bit about my journey before you begin your own.

How it all got started

Last fall, when Mac OS X Public Beta was released, I had this crazy notion of learning how to program in Cocoa. Maybe I was kidding myself at first; I had never done any programming more advanced than C. I didn't know what object-oriented programming was, what it was about, or how it makes modern software possible. Thinking about how people programmed real applications with GUIs and windows and preferences and all that jazz made mush of my mind. How was I going to learn how to do this?

Yet, I had this persistent feeling that it would be possible. The curiosity of what lies beneath Mac OS X's shiny interface drew me in, and I wanted to figure out how it all worked. From what I had heard, Cocoa was the first, best place to start.

Cocoa is Mac OS X, and Cocoa makes truly native Mac OS X applications. I downloaded the developer's tools CD and it wasn't long before I was overwhelmed by everything in there. I saw glimpses of Cocoa's Interface Builder and the über-cool drag-and-drop way of creating interfaces. I saw relics and ghosts of the mysterious NeXT computer lurking on my machine. It was all so cool, yet it was so hard to wrap my brain around it.

In the beginning, learning Cocoa was a series of failing spurts and sputters. It was like trying to start a tired old lawn mower. Documentation on Cocoa and Objective-C was in a sorry state. Apple had been focusing all of its energies on Carbon, and rightly so, trying to get as many developers as possible to write for OS X.

I found web pages here and there telling me how to do "Hello World" in Cocoa and stuff like that. It was a start, but there was nothing linking all the bits and pieces together into a coherent understanding. Soon though, things started falling in place, and the chaos of information in my head crystallized into something intelligible. I had gotten a leg up, and with the world of Cocoa open for me now, I saw amazing possibilities and an astonishing technology.

Soon, I was actually making things that worked with Cocoa! The first thing I found out was that there is no reason why Cocoa should be as hard to learn as it was for me. My hope is that I can help you can see the amazing things Apple has in store with Cocoa without the struggle of learning it like I had. Anyone with any level of programming experience can learn how to program with Cocoa. Cocoa is an advanced and powerful programming environment just waiting to be tapped by the masses of Mac users.

Through this column I hope to show you everything Cocoa has to offer, and how to leverage it for your own needs. In the tradition of O'Reilly, the majority of it will be tutorial; you will get your hands dirty. With Mac OS X and Cocoa we are returning to a time when it was possible to hack around on your system like the way people tinker with cars. This is the spirit and the culture that exists in the open-source community, and Apple is embracing that culture (as much as they can anyway). It's the Unix culture and mentality on a Mac. Learn from it. Use it. Savor it. Everything is changing.

Mac OS X

Mac OS X has a layered system architecture in which increasingly complex sets of services and subsystems depend on and are built upon more fundamental systems. The most fundamental layer in Mac OS X is the Mach kernel environment, where basic control and access to hardware is provided for the operating system.

At the top are the five application environments of Mac OS X: BSD, Classic, Java, Carbon, and Cocoa. Each of these application environments provide a unique set of frameworks, libraries, and services essential for the runtime execution of applications and programs developed with their respective APIs. Each of these application environments provides a unique solution for a unique problem. Classic lets you run your old apps; BSD gives us Unix; Java is Java; Carbon is Carbon; and Cocoa is what we'll learn about in this column.

Frameworks and APIs

Cocoa consists of two object-oriented frameworks called the Foundation and Application Kit. These frameworks are a collection of classes, functions, custom data types, and other useful odds-and-ends that allow the developer to fully direct the advanced functionality of Mac OS X. The bulk of Cocoa is contained in these frameworks, and this column will focus on how to use the Foundation Framework and Application Kit to program in Mac OS X; to speak of these frameworks is to speak of Cocoa.

Frameworks also have APIs for accessing the classes and functions. Cocoa has two: Java and Objective-C. That is, you can write Cocoa programs using either the Java language, or the Objective-C programming language. Objective-C is the native tongue of Cocoa, and as such I will focus on that as our language of choice. Don't worry; Objective-C is easy to learn. In fact, Objective-C is a superset of standard ANSI C, so if you know C, you know 90 percent of Objective-C. Objective-C simply adds to C a set of language extensions that allow you to implement object-oriented programming concepts and techniques. These will be covered in due course.

However, Objective-C lacks functionality beyond the primitive control structures and data types of C. What Objective-C lacks in basic functionality required to build modern software, Cocoa's Foundation Framework fills in. The Foundation Framework is a set of utility classes that provide basic functionality for things like string, text, number, and array handling. It also contains classes that provide access to operating system services such as file system I/O and process management. The Foundation Framework has support for many of the features required by modern software such as Unicode strings and distributed objects. Much of our future discussion will center on the advanced classes of the Foundation Framework.

The layered architecture of Mac OS X is present in all levels of the system, including within Cocoa itself. As such, Cocoa's other framework, Application Kit, is built on the Foundation Framework. The Application Kit fills in the last major void of Cocoa: interface and application management and control.

Application Kit provides a set of classes that define the most common user interface components such as buttons, windows, and scrollbars. However, Application Kit is much more than a simple collection of interface components. It also provides mechanisms for application and document management; there are classes that encapsulate fonts, colors, graphics, and printing tasks; and it also supports OpenGL. Additionally Application Kit has built-in facilities for spellchecking, dragging, and copy-and-paste procedures. All of this is available to your Cocoa application for free, just for trying out Cocoa! Apple has spent a lot of time and effort developing these tools to make the development process that much quicker and easier for you. This is one of the great advantages of Cocoa.

Next time

In the next two columns, I will go into object-oriented programming and Objective-C, respectively. We'll be exercising our minds with the concepts of object-oriented programming and getting our hands greasy with the how-to details of Objective-C. After that, we'll get into writing complete -- albeit simple -- Cocoa applications to show you just how easy it is to get started. As we progress, I will go into individual classes of the Foundation and Application Kit frameworks, progressing to more advanced and more complex concepts. It is my hope that with each column you will be left with enough to keep you going on your own until the next one arrives. This is going to be great! Come back for more!

Michael Beam is a software engineer in the energy industry specializing in seismic application development on Linux with C++ and Qt. He lives in Houston, Texas with his wife and son.

Return to the Mac DevCenter.

Copyright © 2009 O'Reilly Media, Inc.