Learn Objective-C on the Mac - Mark Dalrymple [16]
Therefore, all the examples in this book assume that you are running Leopard or later, and the applications you’ll be writing in this book will run only on Leopard or later. Also, although the iPhone allows for most of the new features of Objective-C 2.0, garbage collection isn’t one of them, so some of what you learn in this book will not really apply to the iPhone at this time.
This Chapter’s Application
After we get through a bit of theory, we’re going to build an application. Our application will have a single window with three buttons. When one of the buttons is clicked, the application will display a label identifying which button it was (Figure 3-1).
Figure 3-1. The Buttons application presents three buttons to the user. When a button is clicked, text is displayed identifying which button it was.
The application is simple, but the mechanisms you will use to create it are the same ones that you will use in nearly all user interaction in Cocoa, so it’s important that you understand what we’re doing in this chapter.
Frameworks, Frameworks Everywhere
In Mac OS X, Apple has grouped code and supporting files together in special folders (or bundles) called frameworks. Frameworks are like the libraries used on most platforms, but they are more flexible because they are folders rather than flat files. Frameworks can contain images, sounds, and movies. They can even contain other frameworks.
Although OS X supports traditional Unix libraries, much of the functionality of the operating system, and nearly all of the functionality that makes OS X unique, is contained in these frameworks. There are literally dozens of frameworks that make up the core operating system, and these are generally grouped by function.
TIP: You can see the frameworks that make up Mac OS X by looking in the folder /System/Library/Frameworks. These frameworks should never, ever be touched, so look only, then quietly back out of the folder so they don’t hear you. Frameworks can be nasty little buggers if you try and touch them. You can also see what third-party and optional frameworks have been installed in your system by looking in /Library/Frameworks. This is where the frameworks that are needed by programs you have installed on your Mac typically reside. Again, look, but don’t touch, or you could mess up something important.
Although there are many frameworks, you’ll spend the vast majority of your programming time in Cocoa using objects from just a handful. In fact, most of the objects you’ll use come from a single framework called (surprise!) the Cocoa framework.
Remember how we told you that frameworks can contain other frameworks? Well, the Cocoa framework is, in fact, just a wrapper around three other frameworks that hold the bulk of the functionality you will use when writing Cocoa applications: the Foundation framework, the AppKit framework, and the Core Data framework.
You will periodically use functionality from other frameworks. For example, you might use the Core Animation framework to do some spiffy animation with your user interface, or you might use the Core Image framework to do some heavy-duty image manipulation. But, the vast majority of the objects you will be using will be from the three frameworks that make up Cocoa. We’ll talk about the Core Data framework starting in Chapter 7, but let’s take a second to briefly look at the other two, which we’ll be working with in this chapter.
The Foundation Framework
The Foundation framework is aptly named. It holds the objects that pretty much everything else is built upon. The Foundation framework is shared between Cocoa and Cocoa Touch. Although the Foundation framework has evolved, many of the objects it contains have been around since the early days of NeXTStep, and their basic usage hasn’t changed all that much. Foundation contains