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?
Objective-c – Rename or refactor files in Xcode
iphoneobjective crefactoringrenamingxcode
Related Topic
- Xcode – How to “add existing frameworks” in Xcode 4
- Ios – Passing data between view controllers
- Objective-c – How to disable ARC for a single file in a project
- Xcode – How to download Xcode DMG or XIP file
- C# – Permissions problem when starting .NET app from .NET service as a different user
- R – CMake: how do you copy private frameworks into the application bundle under OS X
- Xcode – How to completely rename an Xcode project (i.e. inclusive of folders)
- Xcode – Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools)
Best Answer
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, chooseRefactor->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 onViewController
in this code: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.