在Xcode中重命名或重构文件

时间:2021-09-09 01:08:27

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?

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

5 个解决方案

#1


155  

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

重命名文件最安全的方法是使用Xcode的“refactor”命令重构类名。使用该命令可以确保在引用文件或类(包括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.

右键单击接口(.h)文件中的类名,选择Refactor->重命名,Xcode将指导您完成该过程,允许您在提交更改之前预览更改。

More details:

更多的细节:

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

右键单击类名(在@interface后面),选择Refactor->重命名。例如,在代码中右键单击ViewController:

@interface ViewController : UIViewController

在Xcode中重命名或重构文件

Enter the new name for the class:

输入类的新名称:

在Xcode中重命名或重构文件

Xcode will let you review the changes before you commit.

Xcode允许您在提交之前检查更改。

在Xcode中重命名或重构文件

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.

您还可以突出显示类名并使用Xcode的编辑->重构菜单,或者甚至可以将其映射到键盘快捷键。

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.

如果您确实想重命名文件而不影响任何代码,请单击Navigator窗格(左窗格)中的名称,等待片刻,然后再次单击。名称将突出显示,您可以输入一个新的名称。您还可以从导航器中删除、重命名查找程序中的文件并将其重新添加到项目中。

#2


43  

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

适用于Xcode 4.2.1:在project navigator中选择file(默认左面板)并按[tab]

#3


3  

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.

默认情况下,Xcode将重命名一个文件,当您将其重命名为文件导航器(左手outline视图thingy)或使用检查器时。

#4


0  

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.

如果手动重命名一个文件,并使用Find和Replace重命名类名,请确保在目标的构建阶段将该文件添加到编译源中,因为它可能会被删除。

#5


0  

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

选择Refactor->重命名nevan king的答案也适用于Xcode 7.0 beta版(7A120f)

#1


155  

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

重命名文件最安全的方法是使用Xcode的“refactor”命令重构类名。使用该命令可以确保在引用文件或类(包括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.

右键单击接口(.h)文件中的类名,选择Refactor->重命名,Xcode将指导您完成该过程,允许您在提交更改之前预览更改。

More details:

更多的细节:

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

右键单击类名(在@interface后面),选择Refactor->重命名。例如,在代码中右键单击ViewController:

@interface ViewController : UIViewController

在Xcode中重命名或重构文件

Enter the new name for the class:

输入类的新名称:

在Xcode中重命名或重构文件

Xcode will let you review the changes before you commit.

Xcode允许您在提交之前检查更改。

在Xcode中重命名或重构文件

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.

您还可以突出显示类名并使用Xcode的编辑->重构菜单,或者甚至可以将其映射到键盘快捷键。

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.

如果您确实想重命名文件而不影响任何代码,请单击Navigator窗格(左窗格)中的名称,等待片刻,然后再次单击。名称将突出显示,您可以输入一个新的名称。您还可以从导航器中删除、重命名查找程序中的文件并将其重新添加到项目中。

#2


43  

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

适用于Xcode 4.2.1:在project navigator中选择file(默认左面板)并按[tab]

#3


3  

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.

默认情况下,Xcode将重命名一个文件,当您将其重命名为文件导航器(左手outline视图thingy)或使用检查器时。

#4


0  

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.

如果手动重命名一个文件,并使用Find和Replace重命名类名,请确保在目标的构建阶段将该文件添加到编译源中,因为它可能会被删除。

#5


0  

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

选择Refactor->重命名nevan king的答案也适用于Xcode 7.0 beta版(7A120f)