在 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
重命名文件最安全的方法是使用Xcode 的“Refactor”命令重构类名。使用该命令可确保您的代码在引用该文件或类(包括 NIB)的任何地方也得到更新。
右键单击接口(
.h
)文件中的类名,选择Refactor->Rename
,Xcode将引导您完成整个过程,并允许您预览在你做出承诺之前做出改变。更多详细信息:
右键单击类名称(在
@interface
之后)并选择“Refactor”->“Rename”。例如,右键单击以下代码中的ViewController
:输入新名称对于类:
Xcode 会让您在提交之前查看更改。
您还可以突出显示类名称并使用 Xcode 的
Edit->Refactor
菜单,或者如果需要的话,甚至可以将其映射到键盘快捷键。如果您确实想重命名该文件而不影响任何代码,请单击导航器窗格(左窗格)中的名称,稍等片刻,然后再次单击。该名称将突出显示,您可以输入新名称。您还可以从导航器中删除文件,在 Finder 中重命名该文件,然后将其重新添加到您的项目中。
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.
适用于 Xcode 4.2.1:
在项目导航器(默认左侧面板)中选择文件,然后按[tab]
Works for Xcode 4.2.1:
select file in project navigator (default left panel) and press [tab]
单向:选择要重命名的文件,然后按 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 将重命名文件。
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 11.6,尝试使用上述方法将“foo.cpp”重命名为“bar.cpp”不起作用。 REFACTOR 选项呈灰色。相反,请执行以下操作:
屏幕右侧,将出现文件检查器。
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:
Right side of screen, the file inspector appears.
如果您手动重命名文件,并使用查找和替换来重命名类名,请确保该文件已添加到目标的构建阶段的编译源中,因为它可能会被删除。
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.
选择重构->重命名
nevan king 的答案在 Xcode 7.0 beta (7A120f) 上也能正常工作
Choose Refactor->Rename
nevan king's answer is also works fine on Xcode Version 7.0 beta (7A120f)