Replacing Text and Refactoring

Like Xcode 3, Xcode 4 enables you to replace text throughout your project and to make other global changes to your code, such as converting the code to Objective-C 2.0 or creating a superclass from a class. These facilities are referred to as search and replace and factoring.

FInd and Replace Text

To replace text in your source files, choose Replace from the pop-up menu in the search navigator (Figure 6-1). Type the text you want to find in the top text field and press Return, then type the replacement text in the lower text field (Figure 6-2). Note that Xcode does not search for the text if you don’t press Return while the cursor is in the upper text field, so if you get no results after typing your replacement text, put your cursor in the upper text field and press Return. The activity viewer in the workspace toolbar indicates the find operation is in progress (Figure 6-3).

Figure 6-1  Find/Replace pop-up menu
Figure 6-2  Find/Replace results
Figure 6-3  Activity viewer during a Find operation

Click Replace All to replace every occurrence of the searched-for text with the replacement text. To replace a single result, click that result to select it and press Replace. To replace a subset of the occurrences found, use Shift-click or Command-click to make your selection and press Replace.

By default, Xcode creates a snapshot of your project before making the changes; see Take a Snapshot of Your Workspace). To change this preference, select the Snapshots pane in the File > Project Settings dialog. You can also make a manual snapshot before clicking the Replace button (choose File > Manual Snapshot).

Preview Changes

To see what your changes will look like in your source code before deciding which occurrences to replace, click the Preview button below the Replace text box (Figure 6-2). The preview dialog is shown in Figure 6-4.

Figure 6-4  Replacement preview

To select an individual occurrence of the Find text for replacement, click the checkbox for that occurrence in the left pane of the dialog, or click the sliding switch in the center of the right pane. Alternatively, you can select as many occurrences as you wish in the left pane using Shift-click or Command-click, and then press the Space bar to select them. If you decide to replace all the occurrences, click Cancel and then click the Replace All button.

Refactor Your Code

A refactoring operation is one that improves the structure of source code without changing its behavior. You might do this to make it easier to maintain, or as a first step in making further changes to the code. Refactoring in Xcode 4 works much as it does in Xcode 3. Before you can choose a refactoring operation (also called a transformation) from Edit > Refactor or from the shortcut menu in the source editor, you have to select the source code that you want to refactor. Only the refactoring operations appropriate for the selected text are available in the menu.

Once you’ve selected the text and the refactoring operation, Xcode presents a dialog to let you select options and specify symbol names where necessary.

You also have the opportunity to preview the changes and decide which files to include before applying the changes (Figure 6-5). Uncheck a file in the navigator pane to leave it out of the refactoring operation. You can edit your source code directly in the Preview dialog. Any such edits are shown in the preview and included in the refactoring operation.

Figure 6-5  Refactor preview dialog

Possible refactoring operations include:

Refactoring works only with C and Objective-C files.

Before you’ve saved your updated files, you can use the Edit > Undo operation on a per-file basis to back out changes, or you can close the project without saving changes to revert to your original files.