Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
This chapter is from the book

Refactoring Code

Earlier in this hour, you learned how to use Edit All in Scope to change the name of a variable within a method. But what happens if you need to make a more massive change, such as changing the name of a class? Making changes like this is called refactoring and can involve a ridiculous amount of work given the amount of code that needs to be touched, filenames that need to change, and so on. If you find yourself in this situation, however, Xcode offers refactoring features to help get you out of a jam.

To access the refactoring tools, choose Edit, Refactor from the menu bar or right-click in your code to use the Refactor contextual menu item.


To rename a symbol across your entire project, including any files named after that symbol, follow these steps:

  1. Select the symbol in your project.
  2. Choose Edit, Refactor, Rename from the menu bar.

    You are prompted for the new name, and whether to rename any associated files, as shown in Figure 6.21.

    FIGURE 6.21

    FIGURE 6.21 Rename a symbol (variable, class, method, etc. across your project).

  3. After you have made your naming choice, click Preview to show the files that will be changed and the differences between the original and new versions, as shown in Figure 6.22.

    From this window, you can choose which files to change (check or uncheck the boxes in front of their names) and even edit the code directly in the comparison views.

  4. Click Save to make the changes across the entire project.

The remaining refactoring options work in a similar way, but with different effects.


If you find that you have written code for a method but that code would be better suited in its own method or function that is called by the current method, you can use the Extract function. Simply select the code you want to move out of the current method, and then choose Edit, Refactor, Extract.


FIGURE 6.22 Confirm the changes that will be made.

You are prompted for the new function or method name, then presented with a preview of the change. Xcode automatically analyzes the code to determine what parameters are necessary for the new method, and even includes the method/function call in your original method.

Creating Superclasses

Sometimes, when writing a class, you might discover that it would have made more sense for the class you’re building to be a subclass of a larger superclass. To quickly create a superclass, select the class name, and then choose Edit, Refactor, Create Superclass, and, again, follow the onscreen prompts to name and save the new class file.

Moving Up/Down

After creating a superclass (or if you already have one), it might make sense to move some of the variables or methods from your current class into the superclass. Conversely, you might have a superclass symbol that make more sense being contained in one of its subclasses. Use the Edit, Refactor, Move Up/Down functions to move a variable/method into a class’s superclass or a superclass’s variable/method into a subclass.


When you use the Edit, Refactor, Encapsulate action on an instance variable, it creates explicit getters and setters for that variable. In most cases, however, the @property/@synthesize combination should be enough to handle setting up your accessors.

  • + Share This
  • 🔖 Save To Your Account