Developing a Software Product With Xcode

Xcode can help you at each step in the process of developing a software product. That includes steps such as researching Apple technologies; writing and compiling code; and building, linking, testing, debugging, and optimizing the software for your product. By taking a closer look at these steps, you’ll see how Xcode fits into the development process.

This section gives a brief overview of the software development process and how Xcode helps you at each stage in that process. Figure 1-1 shows the typical development process and how it relates to Xcode.

Figure 1-1  Xcode and the software development process

Briefly, the stages of the development process are as follows:

Defining a Product

As you work to define a software product, you typically draw from a number of sources, such as requirements specifications, existing software products, technology documentation, and your own knowledge of what you need to accomplish. In doing your analysis, you don’t want your choices to be restricted by your development environment—rather, you want to have confidence that the IDE supports the product decisions you make.

The following are some of the questions you might ask as you focus on defining a product. In most cases, you’ll find that Xcode can accommodate the requirements identified by your answers.

Of course these are not the only questions that you may need to resolve in the course of defining your product.

Creating a Project

Once you have made your decisions about the type of product (application, library, command-line tool, and so on) and language or languages (C, C++, Objective-C, Java, and others) you plan to use, you’re ready to create a project.

Xcode provides the project as the primary workplace for your software development. When your design has reached the point where it’s time to start working on the code, you can do one of the following:

Once you’ve created an Xcode project, you can add files, add targets, modify target settings, and make any required modifications to develop your software.

Project Organization and Navigation

Before you plunge headlong into working with the project that you have just created, it’s a good idea to familiarize yourself the many ways in which you can organize and quickly access project information and items. Xcode provides many different ways for you to view, organize, and find information in your project, so you can work efficiently.

Organizing a Project

The Xcode project is the primary mechanism for grouping the files and information you need to build one or a set of related products. Within a project, a target specifies the files and other information needed to build a specific product. In addition, Xcode provides groups, as a way to organize information within a project, and to navigate to project files, symbols, and so on.

Xcode defines groups for source code, targets, errors and warnings, bookmarks and other items. You can also create your own groups to help you organize information in ways that make sense to you. To learn more about groups in Xcode, see Groups in Xcode.

Organizing Xcode Projects provides a detailed look at the high-level issues involved in organizing complex projects and targets. It includes Organizing Files, which provides a more detailed look at working with groups.

Project Navigation

Xcode provides convenient navigation at several levels, whether you’re editing source code in multiple files, looking up technical documentation, building, debugging, or performing other tasks.

The project window, described in The Project Window, offers several options for navigation:

  • You can find any project file by first selecting the project group (which reveals a list of all the files in the project), then use filtered searching to find the file you’re looking for.

  • You can define bookmarks to access specific file locations, then use the Bookmarks group to locate specific bookmarks.

  • You can use the Project Symbols group to find a method or function and go directly to its source code.

  • You can use the Errors and Warnings group to go directly to the specific line in your code where an error occurred.

To examine the hierarchy of classes defined in object-oriented languages, Xcode provides a class browser. Using the browser, you can navigate to code (both for Apple frameworks and for classes you have defined) and documentation.

Xcode also provides options for jumping between header and implementation files, jumping to methods or functions within a file, or instantly opening a selected or typed filename. And you can locate text by performing single-file or batch find operations, described in Searching in a Project.

Finding Information

An IDE should help you find the information you need while you’re working on a project. Here are some ways to search for information in Xcode.

Filtered Searching

Filtered searching is available in many places across the Xcode user interface. It refers to the ability to type letters in a search field so that as you type, Xcode filters an associated list, removing any items that don’t match the text you type.

For example, filtering is available in the project window for items such as files, symbols, and errors and warnings; in Info windows (described below) for build settings; and in the Developer Documentation window for symbol names defined by various Mac OS X technologies.

Searching in a Project

Searching for text in your project is a common task that must be fast and convenient. In Xcode, you can perform search and replace operations in a single file, or perform batch searches on multiple files and frameworks. You can search for text, regular expressions, or symbol definitions. You can also define complex search criteria to reuse, and you can store your search results for later reference. To learn more about searching in Xcode projects, see Searching in a Project.

Getting Information About Items in a Project

The project window is the main starting point for getting information about items in your project. For most kinds of information, you won’t need more than a few steps:

  1. Select an item in the project window.

  2. If you need more information about a selected item, open an Info window (by pressing Command-I, choosing Get Info from the File menu, or clicking the Info button in the project window toolbar).

An Info window allows you to view, and in some cases modify, information on items in your project. For example, you can view and change file attributes for one or more selected files. To learn more about Info windows, see Inspector and Info Windows.

Using the Documentation

The importance of documentation in software development can’t be overemphasized. The technical documentation distributed with Xcode provides critical conceptual and reference documentation for creating high-quality, high-performance software for Mac OS X.

At different times in the product cycle, you’re likely to use the documentation to:

  • Learn about the operating system and the technologies it supports

  • Find and compare solutions for technical requirements

  • Read about supported languages and frameworks

  • Look up individual API definitions

  • Learn how to use a required tool

When you install Xcode, technical documentation is installed on your hard drive. You can view it in the Developer Documentation window, accessed through the Help menu.

The Mac OS X documentation distributed with Xcode includes both Apple and open source documentation. Xcode also includes a variety of sample code, installed at /Developer/Examples. Documentation and sample code are also available, free of charge, at the Apple Developer Connection website at http://developer.apple.com. To learn more about viewing documentation in Xcode, see Viewing Documentation.

HeaderDoc

Apple provides the open source HeaderDoc system for creating HTML reference documentation from embedded comments in C, C++, and Objective-C header files. Similar to JavaDoc, the system allows you to document your interfaces and export that information into HTML. For more information on HeaderDoc, see http://developer.apple.com/darwin/projects/headerdoc/.

Editing Files

As you develop your software, you spend a lot of time editing files. To be efficient, you want to be able to work with familiar keystrokes and have access to features such as code completion, automatic indenting, syntax coloring, and so on. You also want to open files quickly, find API documentation, enter API declarations, move between header and implementation files, and work with as many or as few windows as you need.

Xcode handles these requirements through an advanced editor with many customizable features:

For more information on Xcode’s editor, see The Xcode Editor. To learn more about code completion, see Code Completion. To learn how to use an external editor with Xcode, see Using an External Editor.

Resources and Localization

In addition to source code, most projects include resources such as images, sounds, and nib resource files. Many project templates provide default resource files when you create a new project; these resource files are typically organized in the Resources source group. In addition, when you add resource files to a target, Xcode automatically adds them to the correct step of the build process, so that they will be added to your software.

Most Mac OS X software, including applications, plug-ins, and frameworks, is packaged in the form of a bundle. Xcode provides mechanisms both to help you localize resource files that need it, and, when you build your product, to copy localized resources into localized directories in your software bundle. At runtime, your source code can use various APIs provided by Mac OS X to obtain localized information from the bundle.

The following sections provide an overview of how to work with resources, support localization, and provide needed information to the Mac OS X system. To learn more about working with localized files in Xcode, see Customizing for Different Regions.

Information Property List Files

Any Mac OS X software that is packaged in the form of a bundle requires an information property list file named Info.plist. This file, which is critical to configuring your software, contains key-value pairs that specify various information used at runtime, such as the version number. Information in the property list is used by Mac OS X (for example, when launching applications) and is also available to the product that contains the property list.

When you create a new project for a bundled product, Xcode automatically creates an Info.plist file for the project. When you build the product, Xcode copies the property list file into the product’s bundle. The information property list is associated with a target, and you can open an Info window on the target to modify property list values. You can also double-click a property list file in the project window to edit it as an XML text file.

Strings Files

Any text strings in your project that may be displayed to users should be localized. To do this, you place them in strings files, providing one localized variant for each language you support. A strings file, which has the extension strings, stores a series of keys and values, where the values are the strings and the keys uniquely identify the strings. Xcode supports localization with strings files by providing options to make a file localizable and to add files for local variants.

When you build your product, Xcode copies each localized strings file into the appropriate localized directory within the Resources directory of the product bundle. For example, if you localize for French, the French version of a strings file is copied to the French.lproj directory in the bundle. Mac OS X provides various APIs you can use in your source code to obtain localized information from a bundle, such as the correct text string to display for the user’s current locale. For more information, see Internationalization and Localization Guide.

The InfoPlist.strings file is an example of a strings file. It is used to provide localized values for any properties in the Info.plist file that may be displayed to users. When you create a new project for a bundled product, Xcode automatically creates this file in the Resources group. By default, it provides just an English variant, but you can add localized strings files for other languages you support.

Nib Files

A nib file, which has the extension nib, is a resource file that stores user interface information for a product. You create nib files with Interface Builder, which provides a powerful mechanism for graphically laying out the user interface for your software. When you add a nib file to a target, Xcode adds it to the correct stage of the build process, which causes it to be copied into the product’s bundle when you build the product. Your code then has access, at runtime, to user interface items in the nib file.

Resource Manager Files

In previous versions of the Mac OS, applications traditionally used Resource Manager .r (text) and .rsrc (compiled) resource files. While nib files are now the preferred mechanism for defining user interface items, Xcode has built-in support for Resource Manager resources as well.

When you add a Resource Manager resource file to a target, Xcode recognizes it by its extension. When it builds the target, Xcode automatically compiles .r files with the Rez tool. Xcode then copies the resulting .rsrc file into the Resources folder of the product bundle. If you localize any .r files, a .rsrc file is copied into the appropriate localized directory as well.

For related information about these types of resources, see Working With Resources in Porting CodeWarrior Projects to Xcode.

The Edit/Build/Debug Cycle

Once a product has been designed, you spend time in the edit/build/debug cycle: adding or modifying code, building the product, testing it, and repeating these steps, as you find and correct bugs or add additional features. You’ve seen Xcode’s editing features in Editing Files. The following sections describe tools, features, and performance enhancements Xcode provides so that you can take control of the coding cycle.

Tools

The Xcode Tools include the Xcode application, Interface Builder, and a set of integrated compilers, debuggers, and build tools. Along with many tools created by Apple, Xcode incorporates several tools from the UNIX open source community. Together, these tools build on years of software development experience and take advantage of the UNIX-compatible underpinnings of Mac OS X.

Among the open source tools available in the Xcode IDE are the GCC compiler (which supports development in C, C++, Objective-C and Objective-C++), the GDB source code debugger, and the javac and Jikes Java compilers. Apple contributes to improvements in many of these open source tools. Standard UNIX tools are available in subdirectories of /usr.

Tools that originate with Apple include:

  • ld, a linker that supports dynamic shared libraries.

    To work efficiently with dynamic shared libraries at runtime, the Mac OS X runtime architecture provides the dyld (dynamic loader) library manager.

  • xcodebuild, a command-line tool for building Xcode projects.

  • Terminal, an application for doing command-line work in shell windows.

In addition, Xcode includes tools to help you locate hard-to-find bugs, such as memory leaks and bugs in threaded code, as well as tools to help you analyze and optimize the performance of your software.

For more information, see the documents in Tools Xcode Documentation and Performance Documentation.

Building

The Xcode build system provides flexibility and customizability to your workflow. You can control the build process from the toolbar or with keyboard shortcuts, can view errors in the project window or in a separate Build Results window, and can go quickly from errors to the offending line of source code. In the Build Results window, you can control the level of detail—for example, you can choose whether to show warning messages and whether to display build steps.

What takes place at build time depends on several factors. When you create a new project in Xcode, it contains a great deal of build information, including default build settings, build rules that specify tools for processing source files, build styles for development and deployment builds, and build phases for performing the steps of the actual build. To learn more about the information that goes into building a product, see The Build System.

For a simple project, default values are sufficient for Xcode to build your product, performing such steps as compiling, linking, and copying files to the appropriate locations in an application bundle. For projects with special requirements, Xcode provides numerous options for controlling the process. For example, you can set per-file compiler flags or add a step to the build process that executes a shell script to perform special processing.

Debugging

The open source GNU Debugger, GDB, sits behind Xcode’s debugger user interface. It also makes available powerful command-line debugging features. As a result, you can debug at whatever level is most comfortable for you. You can work in the user interface for most of your debugging tasks, but drop down to the command line to take advantage of advanced features that are less commonly needed. For debugging Java products, Xcode communicates directly with the Java Virtual Machine. To learn more about Xcode’s debugger, see Running in Xcode’s Debugger.

Optimizing the Edit/Build/Debug Cycle

Beside the standard features you expect in an IDE, Xcode sports a number of innovative features that can speed up your edit/build/debug cycle and make a big contribution to an efficient workflow:

  • Code completion allows the editor to suggest context-sensitive function names, method names, and arguments as you type.

  • Predictive compilation reduces the time required to compile single file changes by beginning to compile a file while you are still editing it.

  • Distributed builds can dramatically reduce build time for large projects by distributing compiles to available computers on the network. And if you have a dual-CPU machine, Xcode automatically takes advantage of the second CPU for compiling and other operations.

  • Fix and Continue improves your debugging efficiency by allowing you to change the source in a file, recompile just that file, and run the changed code without stopping the current debugging session.

  • ZeroLink shortens link time for development builds and lets you quickly relaunch your application after making changes. (Don’t forget to turn ZeroLink off for deployment builds!)

To learn more about these features, see Optimizing the Edit-Build-Debug Cycle, Using ZeroLink, and Using Fix and Continue.

Analyzing and Optimizing Your Software

Performance optimization should be an integral part of the development cycle, and should include steps such as providing a plan to continually measure and improve the performance of your code. Xcode Tools provide a number of tools to help you fine-tune your software, including tools to:

You can launch your program in many of these tools directly from the Xcode application. For more information on analyzing and optimizing your software in Mac OS X, see the learning paths in Getting Started With Performance.

Customizing Your Work Environment

You can work most efficiently when your development environment complements the way you like to work. Xcode provides many options for customizing its interface, from setting the keystrokes for menu and text-editing equivalents, to configuring the contents and layout of the project window, to setting conventions for editing code.

Preferences

The Xcode Preferences window is the key to customization, and you should spend some time investigating it. It provides access to settings for features such as text editing, syntax coloring, indentation, navigation, building, debugging, source code management, and key bindings for menus and text editing. To learn more about Xcode’s Preferences, see Xcode Preferences.

Customizing the Xcode User Interface

You can customize Xcode’s project window and many other windows too. For example, Xcode provides a number of different project window configurations, or layouts, for you to choose from. You can embed an editor and specify which columns and groups should be shown in the project window. You can also customize toolbars and menus, and control the amount of information shown in the Build Results window.

Working in a Shell

Mac OS X incorporates the FreeBSD variant of UNIX, which includes a command-shell environment. The Terminal application provides an interface for invoking command-line utilities and executing shell scripts. You can build Xcode projects from a shell with the xcodebuild command, in order to, for example, run nightly builds. For more information, see Building From the Command Line.

Xcode also makes it easy to execute shell scripts as part of your development work. You can execute selected text as a shell script or run scripts from the User Scripts script menu. That menu contains default scripts that you can execute as is, or use as examples for scripts you write. You can also write shell scripts that Xcode executes during the build process.

For more information on modifying your work environment and working with shell scripts in Xcode, see Customizing Xcode.