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.
This chapter guides you through building the Currency Converter application and, in the process, teaches you the steps essential to building a Cocoa application.
Overview of the Build Process
Build the Currency Converter Application
Look Up Documentation
Run Currency Converter
Correct Build Errors
Great Job!
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.
To build the Currency Converter application:
Choose Save All from the Xcode File menu to save the changes made to the project’s source files.
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.
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:
Open ConverterController.java
in an editor window.
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.
Close the Developer Documentation window.
Command–double-click the same word. A pop-up menu with a list of method names appears.
Choose [NSCell setFloatValue]
. This time, Xcode jumps to the method declaration in the associated Objective-C header file.
Close the header file.
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.
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:
Open ConverterController.java
.
Delete the semicolon after the selectText
call in the convert
method.
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.
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.
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:
Composing a graphical user interface (GUI) in Interface Builder
Testing a user interface in Interface Builder
Specifying a class’s outlets and actions in Interface Builder
Connecting controller-instances to the user interface via using outlets and actions in Interface Builder
Implementing a model class in Xcode
Building applications and correcting build errors in Xcode
© 2002, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-10-03)