Make Projectwide Changes

Xcode enables you to replace text strings throughout your project and to perform refactoring operations. A refactoring operation—such as creating accessor methods for a symbol or creating a superclass from a class—changes code without changing its behavior.

Replace Text Strings

You can use the search navigator to replace text strings throughout your project or workspace (Figure 9-1). (To make syntax-aware changes to symbol names, including within nib files, use the Rename refactoring operation instead.)

Figure 9-1  Search and replace using the search navigator

The search and replace operation can be customized—for example, to limit the scope of the search or to match the case of letters in the string—and provides a preview that you can use to accept or reject individual replacements.

Replace All Instances of a Text String

The Replace command does pattern matching in text files and replaces instances of the search string with the replacement text you specify.

bullet
To find and replace text in your project
  1. Click the Find pop-up menu in the search navigator and choose Replace.

    image: ../art/find-replace_popup.png
  2. Type the text you want to find in the top text field and press Return.

    image: ../art/find-replace_results.png

    The Activity viewer in the workspace toolbar indicates when the find operation is in progress.

    image: ../art/find-replace_activity.png
  3. Type the replacement text in the lower text field.

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

    To see a preview of the changes before proceeding, follow the procedure in “Replace Selected Instances of a Text String.”

Replace Selected Instances of a Text String

See what your changes will look like in your source code before deciding which occurrences to replace by using the search navigator’s preview feature.

bullet
To preview changes before replacing text
  1. Click the Preview button below the Replace text field.

    The preview dialog appears, showing the current version of each potential replacement in the right column and the new version in the left column. Each arrow points at the version being used. All the potential replacements are selected by default.

    image: ../art/find-replace_preview.png
  2. To change the status (from selected to deselected, or vice versa) of one or more replacements, do one of the following:

    • Click the checkbox for that change in the left pane of the dialog. Click the checkbox next to a filename to change all the items in that file.

    • Click the button in the center of the right pane for each item whose status you want to change.

    • Use Shift-click or Command-click to select items in the list in the left pane, then press the Space bar to change their status.

  3. Click Replace to carry out the replacement of the selected items.

Customize the Search

The search navigator provides Find options to customize a search. For example, you can specify its scope and whether the search should be case sensitive.

bullet
To specify a new scope for a search
  1. In the search navigator, click the magnifying glass in the search field to open the Find options.

    image: ../art/find_options.png
  2. Open the “Find in” pop-up menu.

    The default scope is the workspace. If you have more than one project in the workspace, your projects are listed.

    image: ../art/find_in_popup.png
  3. Choose Custom to create your own custom scopes.

  4. In the Find Scopes dialog, click the Add (+) button to add a new custom scope.

    image: ../art/find_scope_custom_1.png
  5. Use the pop-up menus to specify the scope.

    Depending on your choices, you might also have to fill in or select a value for a third scope parameter.

  6. Select the name of the scope and type a new name for the scope to appear in the “Find in” menu.

    You can reopen this dialog and change the name at any time.

    image: ../art/find_scope_custom_2.png
  7. Repeat this procedure as many times as necessary to define all the custom scopes you want to use. Click Done when you’re finished.

  8. To see your custom scopes, open the “Find in” pop-up menu.

    image: ../art/find_in_popup_2.png

Improve Your Code’s Structure Through Refactoring

A refactoring operation improves the structure of source code without changing its behavior. You refactor to make the code easier to maintain, or as a first step in making further changes to the code.

Select the Source Code to Refactor

Before you can choose a refactoring operation (also called a transformation) from the Refactor submenu in the Edit menu or from the shortcut menu in the source editor, you have to select the symbol or source code that you want to refactor. Only the refactoring operations appropriate for the selected text are available in the menu. The descriptions of the refactoring operations at the end of this chapter specify what code to select for each type of refactoring operation.

When you select the code and the refactoring operation, Xcode presents a dialog to let you select options and specify symbol names where necessary.

Preview the Changes Before Executing the Operation

You have the opportunity to preview the changes and decide which files to include before applying the changes by using the refactor preview dialog (Figure 9-2).

Figure 9-2  The refactor preview dialog

Deselect a file in the leftmost pane of the preview dialog 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.

Before you’ve saved your updated files, you can use the Undo command in the Edit menu on a per-file basis to back out changes.

Run Unit Tests Before and After Refactoring

As part of your refactoring workflow, you should develop unit tests for code you plan to refactor. Run these tests before and after a refactoring to verify that the refactoring operation doesn’t change the behavior of the modified code. For more information on unit tests, see “iOS Configuration.”

Choose a Refactoring Operation

The refactoring operations provided by Xcode include renaming a symbol, extracting a function or method from other code, encapsulating accessors for a symbol, creating a superclass of a class, moving an item up into a superclass, and moving an item down into one or more subclasses.

The Rename refactoring operation changes the name of the selected item throughout your project files.

bullet
To change the name of an item throughout your project
  • Select any symbol except the declaration of a method inside a protocol interface, and choose Rename from the Refactor submenu in the Edit menu (or from the shortcut menu in the source editor). (You can rename such a method if the selection is a definition of such a method in a class that adopts the protocol. That is, if the container of the selection is a protocol, you cannot rename it; if the container of the selection is a class, you can.)

    The Rename refactoring operation differs from the Find and Replace command in that Rename is symbol- and context-aware. Find only matches strings. Rename is particularly useful with properties.

    If you’re renaming a class and you have files that use that class in the filename, select the “Rename related files” option to rename the files as part of the refactoring operation.

The Extract refactoring operation creates a function or method from selected code.

bullet
To extract a function or method
  1. Select code or code and comments within a function or method implementation, and choose Extract from the Refactor submenu in the Edit menu (or from the shortcut menu in the source editor).

    Xcode analyzes the context of the selected code and the variables it uses to determine the generated routine’s parameters and return value.

  2. In the dialog that appears, name the new routine and its parameters, and select whether it is a method or function.

The Encapsulate refactoring operation creates accessors (“get” and “set” methods) for a symbol and changes code that directly accesses the item to use the accessor methods instead.

bullet
To create accessors for a symbol
  • Select a symbol that is an instance variable (ivar) or a member of a structure, and choose Encapsulate from the Refactor submenu in the Edit menu (or from the shortcut menu in the source editor).

The Create Superclass refactoring operation creates a superclass from a class.

bullet
To create a superclass
  1. Select a symbol that is a class defined in your project, and choose Create Superclass from the Refactor submenu in the Edit menu (or from the shortcut menu in the source editor).

    The Create Superclass refactoring operation creates skeletal interface and implementation statements for a superclass placed in the inheritance hierarchy between the selected class and its existing superclass.

  2. In the dialog that appears, name the new superclass and select whether to create new header and implementation files for its interface and implementation statements or put them in the existing file.

The Move Up refactoring operation moves the declaration and definition of an item into the superclass of the class where they currently reside, removing them from their former location.

bullet
To move the declaration and definition of an item into a superclass
  1. 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, and choose Move Up from the Refactor submenu in the Edit menu (or from the shortcut menu in the source editor).

  2. In the dialog that appears, select whether also to move methods that directly access the refactoring item and that are defined in the same class as the refactoring item.

The Move Down refactoring operation 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.

bullet
To move the declaration and definition of an item into one or more subclasses
  1. 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, and choose Move Down from the Refactor submenu in the Edit menu (or from the shortcut menu in the source editor).

    The Move Down refactoring operation moves the declaration and definition of the selected variable into a subclass of the class where it currently resides, removing it from its former location.

  2. In the dialog that appears, specify the subclass into which to move the variable declaration and definition.


Did this document help you? Yes It's good, but... Not helpful...