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).



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.

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.

Possible refactoring operations include:
Rename changes the name of the selected item throughout your project files; select any symbol except except the declaration of a method inside a protocol interface. If you’re renaming a class and you have files that use that class in the file name, check the Rename related files checkbox to rename the files as part of the refactoring operation.
Extract creates a function or method from the selected code; select code or code and comments within a function or method implementation.
Encapsulate creates accessors (Get and Set methods) for the selected item and changes code that directly accesses the item to use the accessor methods instead; select a symbol that is an instance variable (“ivar”) or a member of a struct or union.
Create Superclass creates a superclass from the selected class; select a symbol that is a class defined in your project.
Move Up Moves the declaration and definition of the selected item into the superclass of the class where they currently reside, removing them from their former location; select a symbol that is a method or instance variable in a class, not a category, where the class and superclass are defined in your project.
Move Down moves the declaration and definition of the refactoring item to one or more of the subclasses of the class that declares and defines the item; select a symbol that is an instance variable in a class, not a category, where the class is defined in your project and one or more subclasses already exist.
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.
Copyright © 2011 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2011-08-10