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?
7 Answers
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 the new name for the class:
Xcode will let you review the changes before you commit.
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.

- 112,709
- 45
- 203
- 241
-
1This approach is best, since it will rename class instances held in other files or XIB files. – Kendall Helmstetter Gelner Jun 18 '10 at 01:51
-
This doesn't seem to be working since I've upgraded to 4.3. In the header (.h) file XCode complains that the file is not added to a target - which it should not be, since its a header file. If you try in the .m file, it complains that it is not a symbol (class, function, etc.) – Michael Feb 29 '12 at 19:35
-
@Michael Have you tried it on a test project? I haven't installed 4.3, but it seems to be working OK on 4.2 (just tried it). – nevan king Feb 29 '12 at 20:21
-
Does anybody know where can I find related source code, if it is open source? I think that they used clang tooling for implementing refactoring feature, there are some good interfaces for such functional, but i haven't found the source code. – BergP May 23 '14 at 23:12
-
Unfortunately this makes my Xcode crash. I am running Yosemite Beta so that might be the reason. Hopefully. – CGTheLegend Sep 09 '14 at 22:21
-
At least with Xcode 7.2.1 and Objective-C classes you just have to perform refactor operation and you don't need to rename the file in the project navigator. – Pierre Mardon Mar 15 '16 at 15:16
-
[iSarcasm]I like how in Xcode 9, it almost works the same way but instead, leaves out the step about giving me a dialog where I can rename it, giving me an exciting puzzle game of trying to figure out how to use the UI to rename things.[/iSarcasm] – Dronz Jan 02 '19 at 18:58
-
Nothing happened when I follow Refactor->Rename, using xcode 11.3.1, any help? – Satish Mavani Mar 04 '20 at 15:23
-
"click on the name in the Navigator pane (left pane), wait a second, then click again" for the life of me, this isn't working. I'm spending 10 minutes trying to rename an effing file. – ScottyBlades Mar 23 '21 at 18:45
Works for Xcode 4.2.1: select file in project navigator (default left panel) and press [tab]

- 691
- 5
- 2
-
Thanks. Refactor->Rename was not working with C++ files. This worked. – BlueVoid Mar 18 '13 at 23:18
-
6I think this answer is more deserved of being accepted than the current one. +1 – Morgan Wilde May 01 '13 at 17:14
-
1
-
That allows the file to be renamed, but it doesn't take into account references to the old name within the rest of the code or files. – ILMostro_7 Apr 16 '22 at 14:44
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).

- 744
- 10
- 15
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.

- 162,346
- 23
- 271
- 359
-
Hi, i would like to rename a protocol ( SideToolBarDelegate.h) with ToolBarDelegate.h, but xcode says to me :"SideToolBarDelegate.h is imported or included by a file that is Objective-C++, not Objective-C or C:". How i can rename it please ? Thanks – samir Jan 24 '13 at 15:13
-
it just replace it in one file ( SideToolBarDelegate.h) but i want to replace this name in all the project, like the refactoring function. – samir Jan 24 '13 at 15:20
-
shift-cmd-F, pop down the little options thing to "find & replace". Find and replace... – bbum Jan 24 '13 at 17:40
-
Don't forget to rename things with related names: Body, BodiesViewController, interactingBodies – A strict “find & replace” wouldn't catch the 2nd or 3rd. – willc2 Dec 10 '13 at 06:40
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

- 568
- 5
- 15
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.

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

- 9
- 1
-
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Mayank Jain Jun 17 '15 at 11:39
-
Cause I don't have 50 reputation. That is what I post an answer to response. – Carlos Chang Jun 18 '15 at 09:35