Tutorial: How to Rename a Class using Refactor in Xcode 4 and Xcode 5

Update: Although this post was originally written about Xcode 4 the procedure is the same in Xcode 5

I’ve always found it annoying that the Apple templates for Navigation-based Projects called the root view controller class RootViewController. If you create a really great view controller and then realize it would be useful in another project you have to rename the class and all the files. In XCode 3 this meant renaming the .m, .h and .xib files, using search and replace to rename the class interface and implementation sections and the using Interface Builder to make sure the MainWindow.xib and your newly renamed .xib knew about one and other.* The new refactoring functionality in XCode 4 make this much less painless.

XCode 4 Symbol Navigator

Figure 1: Symbol Navigator

To rename a class begin by switching to the symbol navigator view. You can do this by clicking the icon shown in Figure 1 or choosing View > Navigators > Symbol.

Select the class you want to rename.
Choose Edit > Refactor > Rename….
Enter the new name of the class (see Figure 2).
Click Preview.

XCode 4 Refactoring Class Renaming Action Sheet

Figure 2: New Class Name Action Sheet

The preview action sheet, shown in Figure 3, allows you to view side by side all the proposed changes to your files.

XCode 4 Refactoring Preview

Figure 3: Refactoring Preview

Click Save and you are done!

The only gripe I have is that this process failed to rename the .xib file so I had to do that manually and edit MainWndow.xib’s view controller to point to newly renamed file. Even with this slight glitch, the overall process took less than a minute and is considerably less error-prone that the manual XCode 3 method.

Footnote
* A couple of hours after publishing this article I finally found the refactor menu item in XCode 3. I will write a blog article about it soon.

About idz

A professional software engineer dabbling in iOS app development.
This entry was posted in Tutorial and tagged , , , , . Bookmark the permalink.

3 Responses to Tutorial: How to Rename a Class using Refactor in Xcode 4 and Xcode 5

  1. hieplt says:

    Great tip! i found it very interesting and useful. Thanks very much ^^

  2. Pingback: Renaming A ViewController « iOS Developers

  3. Pingback: Tutorial: Building a Web Browser with UIWebView Revisited (Part 1) | iOS Developer Zone

Leave a Reply