Chapter 2. Tools for Developing Cocoa Applications

There are several applications bundled with Mac OS X that are very useful for writing Cocoa programs. Most of these tools reside in the /Developer/Applications folder, but some reside in the more user-oriented /Applications/Utilities folder. We’ll discuss the most helpful of these tools in this chapter.

Developer Tools

The two most important Mac OS X developer tools by far are Project Builder (PB) and Interface Builder (IB). These tools reside in the /Developer/Applications folder, shown in Figure 2-1.

The developer applications bundled with the Mac OS X developer system

Figure 2-1. The developer applications bundled with the Mac OS X developer system

The third application you’ll need to learn as a Cocoa developer is the gdb debugger. We’ll discuss gdb and how it’s used at the Unix command line and with PB toward the end of this chapter. We’ll also take a quick look at the ObjectAlloc, PropertyListEditor, IconComposer, icns Browser, Console, ProcessViewer, and Terminal applications.

Project Builder

Project Builder is Cocoa’s integrated development environment (IDE), used to manage application development projects. For each application, developers will use PB to create a skeletal application framework, organize the application’s resources, edit the Objective-C source code files, run the compiler and the rest of the build process, control the debugger, add application and document icons, and set up other application features. PB does a lot for developers — it’s a wonderful tool!

The PB menu bar and main window for an under-construction application called Calculator are shown in Figure 2-2. The buttons on the left of the toolbar are for building (compiling), cleaning, running, and debugging applications. The pop-up menu that currently displays “Calculator” is for switching build targets, whereas the buttons on the right of the toolbar are for stepping through an application being debugged. The Groups & Files pane at the left of the window in Figure 2-2 shows the files that make up the project, including the Classes files where you add your application-specific code. The top-right pane shows the output from a compilation process, and the bottom-right pane shows a file (Controller.h) being edited. We’ll discuss all of these PB features in great detail as we build applications throughout this book. We’ll show you how to open a project in PB and also how to use the gdb debugger in PB later in this chapter.

Using PB to build and manage the Calculator application

Figure 2-2. Using PB to build and manage the Calculator application

Interface Builder

Interface Builder is used to create application interfaces (e.g., menus, windows, buttons) and make connections between interface objects. It is also used to connect interface objects to Objective-C data structures called objects .

The IB menu bar and the interface for an under-construction application called Calculator in IB are shown in Figure 2-3. Both the small MainMenu menu bar below IB’s menu bar and the window titled “Calculator” on the left are part of the interface for the Calculator application being built. The window on the right (Cocoa-Views) shows a palette of user-interface objects (e.g., Button, Switch) that can be dragged and dropped into an interface window. The button and text field objects inside the Calculator window were taken from this palette and then modified. The buttons in the toolbar at the top of the Cocoa-Views palette allow developers to access a great variety of user-interface objects for building application interfaces.

Using IB to build the interface for the Calculator application

Figure 2-3. Using IB to build the interface for the Calculator application

In Figure 2-4, our second IB screen shot, the window at the lower left displays icons representing some of the (instance) objects that are part of the Calculator project. For example, the icon labeled “Window” represents the Calculator window, and the icon labeled “MainMenu” represents the Calculator’s menu. The icon labeled “Controller” represents an Objective-C object that has no associated on-screen object such as a button or window. The “connection” line from this Controller icon to the “readout” display in the Calculator window represents a path so that messages can be sent by the Controller object to the readout display to update the output. It’s really just a pointer called “readout” from one piece of code (the Controller object) to another (the text field object). The Info window on the right in Figure 2-4 provides information about existing connections and enables developers to set up new connections. The Info window also displays information about objects’ (e.g., button, Controller) attributes and allows them to be modified.

Connection between on-screen and “code” objects in IB

Figure 2-4. Connection between on-screen and “code” objects in IB

With IB and PB together, it is actually possible to create a working application without any programming whatsoever! Of course, the application won’t do much until you add application-specific code, but it will look good and it will run. We’ll learn more about how to use IB beginning with Chapter 3 and PB beginning with Chapter 4. In the meantime, let’s look at some other important developer tools.

ObjectAlloc

Memory management is one of the most complicated issues that you will encounter as a Cocoa programmer. That’s because unlike in the C programming language, where memory management is completely up to the programmer, or the Java programming language, where memory management is completely automatic, in the Cocoa environment, memory management is semiautomatic . The programming environment does most of the work, but you need to do some of it too. And because memory management is semiautomatic, it’s possible to make mistakes.

You can use the developer application ObjectAlloc to watch a Mac OS X application as it allocates and frees objects. This is not only a useful tool for finding memory leaks and other problems in your programs — it is also a fun way to learn about Cocoa.

To run the ObjectAlloc application, double-click on the ObjectAlloc icon in the /Developer/Applications folder. When the application launches, you’ll be prompted with an Open-like dialog titled “Run”. Within this dialog you need to navigate to the actual executable file in the .app folder (directory) of the application you wish to run. Here’s how:

  1. Launch the ObjectAlloc application, and you’ll get a dialog titled “Run”.

  2. Resize the Run dialog so that at least four columns are visible, as shown in Figure 2-5.

Opening the Clock executable file with ObjectAlloc

Figure 2-5. Opening the Clock executable file with ObjectAlloc

  1. Navigate through the filesystem to the Clock file in the /Applications/Clock.app/Contents/MacOS/ directory. Select this file.

  2. Click Open in the Run dialog, and you’ll see the ObjectAlloc main window.

  3. Click the bevel button with the green “play” arrow on it to start the Clock running under ObjectAlloc.

  4. As the Clock program runs, the bars on the right side of the window change in size, as shown in Figure 2-6. These bars indicate the number of global allocations of objects for the Clock program.

Watching the Clock’s ObjectAlloc application

Figure 2-6. Watching the Clock’s ObjectAlloc application

  1. Click the Auto-sort checkbox at the bottom of the ObjectAlloc window and then the Category column header, so that the table rows remain sorted by category.

  2. Scroll down to where the categories begin with the letters “NS”. Objects with names that begin with “NS” are objects from Cocoa’s Application Kit that are being allocated and destroyed by the Clock program.

  3. Click the Instance Browser tab to look at specific instances of each object.

  4. Click the Call Stacks tab to see what the call stack was at the time that each object was created.

  5. Quit the ObjectAlloc application.

Programs run much more slowly when they are run from within the ObjectAlloc application, so you generally won’t use it to observe programs that you are not debugging. However, when you need to use it, ObjectAlloc can be handy.

PropertyListEditor

The PropertyListEditor application is used to view and edit property lists stored in .plist files. A property list is a list of information that is read by applications while they are launching. The list often contains information such as user preferences, window position and size when the application was last terminated, and so on. We’ll use the PropertyListEditor application later in this chapter.

IconComposer and the icns Browser

The IconComposer application is used to create .icns files that contain application and document icons of various sizes. It’s not a graphics editor like PhotoShop; rather, IconComposer accepts icons that you create in applications such as PhotoShop and bundles them together in one file for use in a Mac OS X application. The icns Browser application simply displays the contents of .icns files. We’ll use IconComposer in our applications later in the book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.17.12