在 Xcode 中重命名类: Refactor... 变灰(禁用)。为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/992381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:04:13  来源:igfitidea点击:

Rename a class in Xcode: Refactor... is grayed out (disabled). Why?

iphonexcoderefactoring

提问by Elliot

Why is Refactor...grayed out (disabled) in Xcode?

为什么Refactor...在 Xcode 中变灰(禁用)?

I'd like to rename a class.

我想重命名一个类。

回答by Noah Witherspoon

Select the class's symbol in its header file - i.e. the bit just after @interface. Then the refactoring stuff should be enabled.

在其头文件中选择类的符号 - 即@interface. 然后应该启用重构的东西。

回答by Volker Voecking

If you are using Objective-C++ (i.e. mix Objective-C with C++ code) then refactoring is disabled in xcode since it does not support refactoring of C++ code.

如果您使用的是 Objective-C++(即混合 Objective-C 和 C++ 代码),那么在 xcode 中禁用重构,因为它不支持 C++ 代码的重构。

回答by klaaspieter

Refactor might also be disabled if affected files (most likely the file with your class in it) are not saved.

如果未保存受影响的文件(很可能是包含您的类的文件),也可能会禁用重构。

回答by Adam

I've been using Xcode for 5 years now, and refactoring has never worked correctly (even xcode 4.6 has major bugs where it WILL corrupt your source code!).

我已经使用 Xcode 5 年了,重构从来没有正常工作(即使 xcode 4.6 也有严重的错误,它会破坏你的源代码!)。

The workaround has always been (still works 100%, even in cases where Apple's code fails)

解决方法一直是(即使在 Apple 的代码失败的情况下,仍然可以 100% 工作)

  1. use shift-command-f to find all uses of the file
  2. select "replace" in the search settings
  3. "replace-all"
  4. do the following for the .h file, and REPEAT IT FOR THE .m FILE (if you have one):
    1. right click the original file, and select "show in finder"
    2. delete the file from xcode (select "delete references only" when asked)
  5. rename the .h (and .m if you have one) in Finder
  6. in Xcode, select "Product -> (hold down Alt) -> Clean Build Folder"
  7. quit xcode (you can usually get away with not doing this - but NOTE: there are some other MAJOR bugs in Xcode where it crashes itself if you don't do this)
  8. re-open xcode
  9. drag/drop the .h and .m back into Xcode
  10. wait a few seconds (some of Xcode's core methods are asynchronous - allowing it to corrupt your project)
  11. finally, when it seems to be doing nothing (and your hard disk isn't making any noise any more): cmd-b to re-build
  1. 使用 shift-command-f 查找文件的所有用途
  2. 在搜索设置中选择“替换”
  3. “全部替换”
  4. 对 .h 文件执行以下操作,并为 .m 文件重复此操作(如果有):
    1. 右键单击原始文件,然后选择“在查找器中显示”
    2. 从 xcode 中删除文件(在询问时选择“仅删除引用”)
  5. 在 Finder 中重命名 .h(和 .m,如果你有的话)
  6. 在Xcode中,选择“产品->(按住Alt)->清理构建文件夹”
  7. 退出 xcode(您通常可以不这样做而逃脱 - 但注意:Xcode 中还有一些其他主要错误,如果您不这样做,它会自行崩溃)
  8. 重新打开 xcode
  9. 将 .h 和 .m 拖放回 Xcode
  10. 等待几秒钟(一些 Xcode 的核心方法是异步的——允许它破坏你的项目)
  11. 最后,当它似乎什么都不做(并且您的硬盘不再发出任何噪音)时: cmd-b 重新构建

I have a 100% success rate with this method. I just tried refactoring with Apple's "Refactor -> Rename" in latest xcode and it failed - again!

我用这种方法有 100% 的成功率。我刚刚尝试在最新的 xcode 中使用 Apple 的“重构 -> 重命名”进行重构,但它再次失败了!

(this time with the incorrect error: "Unable to determine the language of", one of those error messages where Apple put the wrong text in place)

(这次出现了不正确的错误:“无法确定语言”,这是 Apple 放置错误文本的错误消息之一)

回答by offset

I'm going to my project at finder, then change files name by get info.

我将在 finder 中访问我的项目,然后通过get info更改文件名。

After that, at xcode -> Project NavigatorI delete the files.

之后,在xcode -> Project Navigator我删除文件。

At end, I click right on the class and Add files to ..., and add these files again.

最后,我在类上单击右键并将文件添加到 ...,然后再次添加这些文件。

It worked for me.

它对我有用。

回答by user591345

For me I realized Refactor was disabled because the Xcode project I had opened was referencing a Base SDK that was missing. Edit Project Settings and in the Build tab set the Base SDK to one that you have (like for me this was iOS 4.2). This enabled Refactor for me.

对我来说,我意识到 Refactor 被禁用了,因为我打开的 Xcode 项目引用了一个缺失的 Base SDK。编辑项目设置并在“构建”选项卡中将 Base SDK 设置为您拥有的一个(对我来说这是 iOS 4.2)。这为我启用了重构。

回答by carlos_ms

Also, it could happen that you renamed the filename for the class, either outside xcode or by ctrl-clicking the filename and then renaming it. xcode refuses to refactor if filename does not match with the class name.

此外,您可能会在 xcode 之外或通过按住 ctrl 单击文件名然后重命名该类的文件名。如果文件名与类名不匹配,xcode 将拒绝重构。

回答by easytarget

Go to your class' header file and find the line that looks similar to this:

转到您班级的头文件并找到与此类似的行:

@interface YourClassName

Right click on the class symbol (e.g. YourClassName) and you should be able to select Refactor -> Rename...

右键单击类符号(例如 YourClassName),您应该能够选择 Refactor -> Rename...

I just tried this and it works in Xcode 5.

我刚刚试过这个,它在 Xcode 5 中工作。

回答by gdavis

Had this problem as well. I ran through trying to find missing SDKs, saving files, and looking for Objective-C++ code as mentioned above, and all it took to fix my machine was rebooting XCode.

也有这个问题。如上所述,我尝试查找丢失的 SDK、保存文件并查找 Objective-C++ 代码,修复我的机器所需的只是重新启动 XCode。

Seems a little buggy still.

看起来还是有点小毛病。

BTW, this was for XCode 4.0.1

顺便说一句,这是针对 XCode 4.0.1

回答by Chris

Refactoring works If you first change the file name in the project navigator.

重构工作如果您首先在项目导航器中更改文件名。