< Previous PageNext Page > Hide TOC

Legacy Documentclose button

Important: The Java API for Cocoa is deprecated in Mac OS X version 10.4 and later. You should use the Objective-C API instead. For a tutorial on using Cocoa with Objective-C, see Cocoa Application Tutorial.

Building Currency Converter

This chapter guides you through building the Currency Converter application and, in the process, teaches you the steps essential to building a Cocoa application.

In this section:

Overview of the Build Process
Build the Currency Converter Application
Look Up Documentation
Run Currency Converter
Correct Build Errors
Great Job!


Overview of the Build Process

You start the build process by clicking the Build toolbar item in the Xcode project window. During this process, Xcode coordinates the compilation and linking tasks that result in an executable file. It also performs other tasks needed to build an application.

While building a project, Xcode invokes the compiler, passing it the source code files of the project. The compilation of these files produces object files for the architectures specified for the build.

In the linking phase of the build, Xcode executes the static linker, passing it the libraries and frameworks to link against the object files. Frameworks and libraries contain precompiled code that can be used by any application. Linking integrates the code in libraries, frameworks, and object files to produce the application executable file.

Xcode also copies nib files, sound files, image files, and other resources from the project directory to the appropriate locations in the application bundle. An application bundle is a directory that contains the application executable and the resources needed by that executable. This directory appears as a single file in the Finder; it can be double-clicked to launch the application.

Build the Currency Converter Application

To build the Currency Converter application:

  1. Choose Save All from the Xcode File menu to save the changes made to the project’s source files.

  2. Click the Build toolbar item in the project window.

The status bar at the bottom of the project window indicates the status of the build. When Xcode finishes—and encounters no errors along the way—it displays “Build succeeded” in the status bar. If there are errors, however, you need to correct them and start the build process again. See Correct Build Errors for details.

Look Up Documentation

Xcode gives you access to ADC Reference Library content. You can jump directly to documentation and header files while you work on a project. Try it out:

  1. Open ConverterController.java in an editor window.

  2. Option–double-click the word setFloatValue in the code. (Hold down the Option key and double-click the word.) The Developer Documentation window appears with a list of relevant method names in its detail view. The Java language version of setFloatValue in NSCell is highlighted in the detail view, and its associated HTML-formatted documentation appears in the content pane. This reference-library access system provides a fast way to get to reference material. Read more in Expanding on the Basics.

  3. Close the Developer Documentation window.

  4. Command–double-click the same word. A pop-up menu with a list of method names appears.

  5. Choose [NSCell setFloatValue]. This time, Xcode jumps to the method declaration in the associated Objective-C header file.

  6. Close the header file.

Run Currency Converter

Your hard work is about to pay off. Since you haven’t edited any code since the last time you built the project, the application is ready to run. Notice that the Build and Build and Run toolbar items are pull-down menus containing other useful commands. Click and hold the Build and Run toolbar item and choose Run.

After the Currency Converter application launches, enter a rate and a dollar amount and click Convert. Now, select the text in a text field and choose the Services submenu from the Application menu. The Services menu lists other applications that can operate on the selected text.

Quit Currency Converter by choosing Quit Currency Converter from the application menu.

Correct Build Errors

Of course, rare is the project that is flawless from the start. For most applications you write, Xcode is likely to catch some errors when you first build them. Thankfully, Xcode offers tools to help you catch those bugs and move on.

To get an idea of the error-checking features of Xcode, introduce a mistake into the code:

  1. Open ConverterController.java.

  2. Delete the semicolon after the selectText call in the convert method.

  3. Click the Build toolbar item.

Uh-oh! Something is amiss. You can now see that the left column of your code contains one error indicator.

While the error indicator helps you understand the location of the error, you may want to examine the nature of the problem. In the Groups & Files list, disclose the Errors and Warnings group if it’s not already disclosed. Xcode lists the files that contains build errors. In this case, the ConverterController.java file is the only file with a problem.

Select the file in the Errors and Warnings group to display the error. Xcode displays information about the error in the detail view, as shown in Figure 3-1.


Figure 3-1  Identifying build errors


Fix the error in the code and build the application again. The Errors and Warnings group clears and the status bar indicates that the build is successful.

Great Job!

Although Currency Converter is a simple application, creating it illustrates many of the concepts and techniques of Cocoa development. Now you have a much better grasp of the skills you need to develop Cocoa applications. Let’s review what you learned:



< Previous PageNext Page > Hide TOC


© 2002, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-10-03)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.