Rename or refactor files in Xcode

IphoneObjective CXcodeRefactoringRenaming

Iphone Problem Overview


Is it possible to rename/refactor a class file and have it rename the actual file on disk? If so, how can I do this?

Iphone Solutions


Solution 1 - Iphone

The safest way to rename files is to refactor the class name using Xcode's "Refactor" command. Using that command makes sure that your code is also updated anywhere that references the file or the class (including NIBs).

Right-click on the class name in the interface (.h) file, choose Refactor->Rename, and Xcode will guide you through the process, allowing you to preview changes before you commit to them.

More details:

Right click on the class name (after @interface) and choose Refactor->Rename. For example, right click on ViewController in this code:

@interface ViewController : UIViewController

enter image description here

Enter the new name for the class:

enter image description here

Xcode will let you review the changes before you commit.

enter image description here

You can also highlight the class name and use Xcode's Edit->Refactor menu, or even map it to a keyboard shortcut if you want.

If you actually want to rename the file without affecting any code, click on the name in the Navigator pane (left pane), wait a second, then click again. The name will be highlighted and you can type in a new one. You can also delete from the Navigator, rename the file in the Finder and re-add it to your project.

Solution 2 - Iphone

Works for Xcode 4.2.1: select file in project navigator (default left panel) and press [tab]

Solution 3 - Iphone

By default, Xcode will rename a file when you rename it in either the file navigator (left hand outline view thingy) or when you use the inspector.

Solution 4 - Iphone

One Way: Select the file you want to rename and then press Tab. I mostly use this, however, Xcode sometimes fails to rename it. So what I'd then is

Another Way: Select the file. Open the Inspectors Area (on the top-right corner). And then open up File Inspector. There you'll find the name field, where you can edit file's name. (See screenshot).

enter image description here

Solution 5 - Iphone

Xcode 11.6, attempting to rename "foo.cpp" to "bar.cpp" does not work using the above methods. the REFACTOR option is greyed out. Instead, do the following:

  • In the Project Browser, find the file to rename, ie: foo.cpp
  • Right (2finger) click on foo.cpp
  • Select, "Show in File Inspector"

Right side of screen, the file inspector appears.

  • In the Name field, type your new filename, press ENTER

Solution 6 - Iphone

If you renamed a file manually, and used Find and Replace to rename the class name, make sure the file is added to Compile Sources in Build Phases of your target, as it will probably be removed.

Solution 7 - Iphone

Choose Refactor->Rename nevan king's answer is also works fine on Xcode Version 7.0 beta (7A120f)

Recommended Renaming Solutions

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSheehan AlamView Question on Stackoverflow
Solution 1 - Iphonenevan kingView Answer on Stackoverflow
Solution 2 - IphoneDaveView Answer on Stackoverflow
Solution 3 - IphonebbumView Answer on Stackoverflow
Solution 4 - IphoneKareem JeiroudiView Answer on Stackoverflow
Solution 5 - Iphoneuser3696153View Answer on Stackoverflow
Solution 6 - IphonejclvView Answer on Stackoverflow
Solution 7 - IphoneCarlos ChangView Answer on Stackoverflow