简体   繁体   中英

Rename or refactor files in Xcode

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?

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.

适用于 Xcode 4.2.1:在项目导航器(默认左侧面板)中选择文件并按[tab]

默认情况下,当您在文件导航器(左侧大纲视图)中或使用检查器重命名文件时,Xcode 将重命名文件。

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).

在此处输入图片说明

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

如果您手动重命名文件,并使用查找和替换重命名类名,请确保该文件已添加到目标的构建阶段中的编译源,因为它可能会被删除。

选择 Refactor->Rename nevan king's answer 在 Xcode Version 7.0 beta (7A120f) 上也可以正常工作

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM