Workshop 19: Rename a Swift and Objective-C Class File

After creating an iOS project, one of the first thing you will do is rename the Swift or Objective-C class file. You will learn how to do that in this workshop.

Say you wanted to rename a Swift class file called ViewController class file to MainViewController, you cannot use Xcode’s Refactor menu item to rename it. If you do, Xcode will display this message on your screen:

xcode6-rename-aclass-error

So to rename a swift class file, you have to do the following:

1. In the Navigator pane, double-click the swift class file and change its name to what ever you like-see image below.

2. In the code editor pane, double-click the class name; for example, ViewController and change it to what ever you like-see image below.

rename-swift-class-file

Rename an Objective-C Class File

If the class file you want to rename is an Objective-C class, then perform these steps:

1. In the Navigator pane, select the header file of the class you want to rename; for example, ViewController.h, then highlight the class name in the code editor pane:

xcode6-rename-aclass-step1

2. Select Refactor | Rename… from Xcode’s Edit menu:

xcode6-rename-aclass-step2

3. Enter a name in the Rename ViewController box. Make sure to tick the Rename related files checkbox before clicking the Preview button.

xcode6-rename-class-step3

4. Once you see the Preview window on your screen, click the Save button.

xcode6-rename-class-step4

That’s it. Now you know how to rename a Swift and Objective-C class file.

  • Kevin Bradley

    I’m using Xcode 7, Swift 2.0. I could do it successfully like this:

    Rename your view controller’s class name at the top of your view controller class and the file name as above.
    Select the scene for the view controller you are renaming, either in the Storyboard or Document Outline.
    Go to the Identity Inspector, select the name you chose for the view controller in the Class dropdown.