Preface

Welcome!

Building Cocoa Applications describes how to write Objective-C programs for computers running the Mac OS X operating system, using the object-oriented Cocoa application framework. The book covers a wide range of technologies:

  • The Aqua graphical user interface, Cocoa developer tools, object-oriented concepts, and the Objective-C language

  • Cocoa programming and graphics concepts: nibs, icons, delegation, resizing, events, responders, tasks, pipes, color, Rich Text, the mouse, zoom buttons, pasteboards, modal sessions, and drag-and-drop

  • The Cocoa environment: Darwin and the Window Server, the document-based architecture, the Quartz drawing system, Cocoa’s preferences and defaults systems, and facilities for saving, loading, and printing

Building Cocoa Applications is a no-nonsense, hands-on book that’s intended for serious developers. It’s filled with extended examples illustrating complete applications written in Objective-C. As you proceed through the book, you’ll take a step-by-step approach to building a series of applications of increasing complexity, adding features as you go.

Although we do not assume prior knowledge of the Macintosh or any other window-based operating environment, we do assume some familiarity with programming in general and the ANSI C language in particular.

Our primary goal is to get you up and running as quickly as possible. If you carefully read this book from cover to cover and diligently build the sample applications along with us, we’re confident that you’ll soon be writing your own sophisticated Cocoa graphics applications.

While this book is fast moving, we start by laying a solid foundation. Part I of the book explains how to use Aqua, the Mac OS X graphical user interface (GUI), and describes the Cocoa developer tools you’ll use to build applications. It also introduces two simple but complete Cocoa applications — one built with Cocoa’s Interface Builder tool and one built without it. Parts II, III, and IV are organized by application: we’ll build three major, highly useful graphics applications and, in doing so, teach you how to build your own applications with Cocoa. The applications we will build are:

Calculator (Part II)

A simple, four-function calculation application that’s similar to the calculator that comes with Mac OS X

MathPaper (Part III)

An application that is similar to a word processor but that solves equations you supply

GraphPaper (Part IV)

A more complex, multithreading application that graphs a formula in two dimensions

The first chapter in each of these three parts introduces the application and builds its most basic functionality. Each subsequent chapter adds a new layer of functionality. For example, Chapter 6 shows how to add an icon to the Calculator application so it will display nicely in the Finder, Chapter 12 enhances MathPaper so it displays Rich Text, and Chapter 19 adds zoom buttons to GraphPaper.

We’ll also build numerous additional simple applications throughout the book to demonstrate specific features of Cocoa and Mac OS X. You can build all of these applications right along with us — we provide simple but complete instructions on how to do whatever is necessary. Code for all of the applications we’ll build is provided on the O’Reilly web site (see Section P.5, later in this Preface).

Cocoa and Mac OS X

Cocoa is an object-oriented development environment that runs in the Mac OS X environment. Cocoa software has previously been bundled with the Mac OS X user system on a separate developer CD-ROM, but newer systems come with a package that users can install — the package name is Developer.mpkg, and it’s found in /Applications/Installers/Developer Tools. Although there are many ways to write programs for Mac OS X, we think you’ll find Cocoa is both the easiest and the most powerful.

Mac OS X and Cocoa Components

From the user’s point of view, Mac OS X is a unified operating system and graphical operating environment that makes computers easy to use. It includes:

Aqua

Apple’s revolutionary GUI, which is both visually pleasing and very easy to use.

Quartz

A comprehensive two-dimensional drawing system that can be used to display text and graphics on a computer screen or on a printer and to create Adobe Portable Document Format (PDF) files.

The Finder

A graphical interface to the computer’s filesystem and to running applications.

Mail, TextEdit, Terminal, Console

Some of the Apple-supplied applications included in Mac OS X.

Foundation

An underlying set of operating-system services that are provided to Carbon, Cocoa, and Java programs.

System Preferences and the defaults system

Enable users to store their preferences for individual programs without having to directly modify special files stored in their home directories.

The HFS+ filesystem and UFS filesystem

Organize the way that files and folders are stored on the computer’s disks.

TCP/IP networking

Allows Macintosh computers to communicate with each other and with the Internet.

Darwin

The underlying Unix operating system on which Mac OS X is based.

Figure P-1 shows the relationship between these technologies in the Mac OS X operating system.

The Mac OS X architecture

Figure 1. The Mac OS X architecture

The Mac OS X operating system can run programs with many different kinds of user interfaces, including:

  • Programs written with the Cocoa development environment. These programs display natively with the Aqua user interface.

  • Programs that use a restricted part of the Mac OS 9 interface called Carbon. These programs also display with the Aqua user interface.

  • Programs written in the Java programming language. Java programs can use either the Cocoa or Swing application frameworks.

  • Programs written for the Mac OS 7, 8, and 9 operating systems. These programs are run in the Mac OS X “Classic” environment and appear as they would on a Macintosh computer running Mac OS 9.

  • Programs written for the underlying Unix operating system. These programs either do not display a user interface at all, or implement a character-based interface from the Unix command line.

This book focuses on the Cocoa development environment, but we will mention the other user interfaces as necessary. From the programmer’s point of view, Cocoa includes two distinct frameworks:

Foundation

A collection of Objective-C classes for managing memory, interfacing with the computer’s operating system, and performing other functions that are independent of the GUI.

Application Kit (AppKit)

A collection of Objective-C classes that give Cocoa its distinctive look and feel.

By using Cocoa, your programs automatically get the Aqua look and feel. Although every application is different, Cocoa makes it easier for all applications to work in similar ways, which in turn makes it easier for people to learn new applications.

Cocoa also makes it easier for applications to work with each other. Because of Cocoa’s object-oriented nature, Cocoa applications can easily provide services and special functions to other applications running on the same computer or across the network.

Using Cocoa speeds your development time. Programmers with just a few months of experience with Cocoa report that they can develop a Cocoa application 3 to 10 times faster using the Foundation and Application Kit than they can using other application frameworks such as PowerPlant, Qt, or Microsoft’s Foundation Classes. For many corporations, this improved productivity justifies the decision to deploy Macintosh computer systems.

Object-Oriented Programming

Writing programs for Cocoa is similar to, and yet significantly different from, writing programs for other environments. As with other modern application development environments, you write Cocoa programs by building systems of related but distinct parts, or objects , and connecting them together to form an integrated whole. Confining different aspects of a program to different pieces makes those pieces easier to design, implement, debug, and reuse. This is what is known as object-oriented programming (OOP).

Unlike development systems based on the C++ programming language (such as Microsoft’s Foundation Classes and Code Warrior’s PowerPlant), however, Cocoa is built on top of the Objective-C programming language. As we shall see, Objective-C is a simpler and more powerful object-oriented extension of the C programming language than C++.

Cocoa embodies the principles of object-oriented programming from its user interface down to its very core. This greatly simplifies the task of building applications for Mac OS X. The down side is that it makes the Cocoa environment very different from the environments to which most programmers are accustomed, and consequently, although it is an easy-to-program environment, the initial learning curve is quite steep.

Cocoa Versions

All the examples in this book were developed and tested under Mac OS X Version 10.1. Although future versions of Cocoa are sure to add new features and visually change some of the user interfaces, Objective-C’s dynamic binding all but assures that any Cocoa program developed under Version 10.1 will continue to run on future versions of the Macintosh operating system. Furthermore, we’ve tried to focus the subject matter of this book mainly on the underlying concepts and features of the operating system — and many of these haven’t changed much since the initial release of NeXTSTEP 1.0 in 1989. For these reasons, this book is likely to stay in print, and be very useful, for quite some time after it is published.

Cocoa, Objective-C, and Java

Although the Cocoa Foundation and the Application Kit are written in Objective-C, Cocoa programs can be written in either Objective-C or Java. This freedom comes from the fact that Java and Objective-C have very similar models of object-oriented programming, which has allowed Apple to create a “Java bridge” that allows Java objects to invoke Objective-C methods and vice versa. Java can even be used to subclass Objective-C classes!

Despite the ability to intermix Objective-C and Java within a single program, this book focuses solely on the Objective-C programming language. Objective-C is the native environment of the Foundation and the Application Kit, and it is generally easier to debug Cocoa programs written in Objective-C than to debug the same programs written in Java. Furthermore, there are performance considerations: Cocoa-based programs run faster if they are written in Objective-C than if they are written in Java.

One significant advantage of Java over Objective-C is the large number of third-party class libraries that are now available for Java. If you need to use one of these libraries, you can benefit from using Java for some or all of your Cocoa applications. In particular, Apple’s Enterprise Objects Framework now supports only the Java-based application programming interface (API).

One of the primary differences between Java and Objective-C is memory management: Java has automatic garbage collection, whereas Objective-C has a reference-count-based memory-management system. The Objective-C system is cruder, but it gives programmers greater control and generally produces applications that run faster.

For detailed information on programming Cocoa applications in Java, see the upcoming O’Reilly book Cocoa and Java. (And for those interested in writing Perl applications for this new platform, watch for Programming Cocoa Applications with Perl, also coming soon from O’Reilly.)

The Foundation Classes

Many of the Cocoa class libraries are actually implemented with the Apple Foundation library. The Foundation provides a series of highly efficient, low-level services for building advanced applications. Built into the Foundation is support for Unicode strings, XML property lists, URL resources, preferences, and other key Mac OS X technologies.

The Foundation library is used by both Cocoa and Carbon, and it provides for improved compatibility between applications written with these two application frameworks.

You can find out more about Foundation by reading the documentation that comes with the developer tools (it is installed in /Developer/Documentation/CoreFoundation). For the latest updates, check out the version at:

http://developer.apple.com/techpubs/macosx/Cocoa/Reference/Foundation/ObjC_classic/FoundationTOC.html

Drawing with Quartz

One of the most important differences between Mac OS 9 and Mac OS X is the way these systems draw on the computer’s screen. Mac OS 9 does all of its drawing with Apple’s QuickDraw APIs. Mac OS X, in contrast, does its drawing with Quartz.

Quartz integrates into the Macintosh operating system many advanced features that were previously available only in Apple’s QuickDraw GX and NeXT’s Display PostScript drawing environments. Quartz also brings native support for Adobe’s PDF. Programs written for Quartz can display PDF files as a native file type. They can also capture their drawing commands and generate PDF files directly, without having to use a PDF Writer or Distiller program.

Besides looking great, Quartz makes it much easier to move completed documents from Mac OS X systems to computers running the Windows or Unix operating systems, because both of these systems have freely available readers that will display PDF-encoded files.

You can find out more about Quartz by reading the documentation that comes with the developer tools (it is installed in /Developer/Documentation/CoreTechnologies/graphics/Quartz2D). For the latest updates, check out the version at:

http://developer.apple.com/quartz/

Mac OS X and Classic Mode

Of course, Apple couldn’t release a new operating system for the Macintosh and not allow existing Macintosh programs to run on it, so Mac OS X also supports the “Classic” Macintosh environment. If you double-click on the icon for an application that runs on older Mac OS computers, a Mac OS X system will launch a copy of Mac OS 9 within Mac OS X. When you activate this application, the desktop will take on the look and feel of the Mac OS 9 environment. It’s weird, but you can run those old applications quite well in Classic mode, and it works better than an emulation because it’s actually a full version of Mac OS 9.x running in protected memory space under Mac OS X.

However, while Mac OS X systems will run Classic and Carbon-based applications, the future is Cocoa. Apple says that all new applications for the Mac should be written with the new Cocoa APIs, rather than with the old ones. And because it is so easy to use Cocoa, why would you want to do anything else?

Get Building Cocoa Applications: A Step by Step Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.