在Xcode中重命名或重构文件

She*_*lam 76 iphone xcode refactoring objective-c renaming

是否可以重命名/重构类文件并让它重命名磁盘上的实际文件?如果是这样,我该怎么做?

nev*_*ing 165

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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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.

如果您确实想要在不影响任何代码的情况下重命名文件,请单击"导航器"窗格(左窗格)中的名称,等待一秒钟,然后再次单击.名称将突出显示,您可以键入新名称.您也可以从导航器中删除,在Finder中重命名文件并将其重新添加到项目中.

  • 男人 - 我想知道如何做这个年龄! (2认同)

小智 50

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

  • 我认为这个答案比当前的答案更值得被接受.+1 (4认同)

Kar*_*udi 5

一种方法:选择要重命名的文件,然后按Tab。我主要使用这个,但是,Xcode 有时无法重命名它。所以我接下来要做的是

另一种方法:选择文件。打开检查器区域(位于右上角)。然后打开文件检查器。在那里您将找到名称字段,您可以在其中编辑文件的名称。(参见屏幕截图)。

在此输入图像描述