无法在 Xcode 9 中对我的 Swift 文件进行重构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46441511/
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-15 10:19:16  来源:igfitidea点击:

Unable to do refactoring on my Swift file in Xcode 9

xcodexcode9

提问by CodeChanger

I am using XCode9and I am trying to do Refactoringon my Swiftbased file but every time I am getting below error:

我正在使用XCode9并且正在尝试对基于我的文件进行重构Swift但是每次出现以下错误时:

Refactoring engine ranges didn't match initial ranges

重构引擎范围与初始范围不匹配

enter image description here

在此处输入图片说明

I don't why it's not matching initial range. Does anyone have any solutions for this issue?

我不知道为什么它不匹配初始范围。有没有人对此问题有任何解决方案?

Thanks in Advance!

提前致谢!

回答by akroy

This has not been resolved as of XCode 9.2. Restarting fixes for me.

自 XCode 9.2 起,此问题尚未解决。为我重新启动修复。

回答by manman

UpdateFor some reason, it's not happening for me anymore. I noticed also whenever I don't let indexing finishes before trying to start doing a refactor or tap on refactor many times, still see the error, but not permanent anymore.

更新出于某种原因,它不再发生在我身上。我还注意到,每当我在尝试开始重构或多次点击重构之前不让索引完成时,仍然会看到错误,但不再是永久性的。

I asked an engineer at WWDC 2018 about this issue. This issue was happening for me in only one project in my workspace. Other projects in the same workspace works fine. At the moment, there's no solution to this issue. If you want to help Apple to fix this, you can close your Xcode and run following command in terminal:

我向 WWDC 2018 的工程师询问了这个问题。这个问题只发生在我工作区的一个项目中。同一工作区中的其他项目工作正常。目前,没有解决此问题的方法。如果你想帮助 Apple 解决这个问题,你可以关闭你的 Xcode 并在终端中运行以下命令:

SOURCEKIT_SERVICE_LOG=3 /Applications/Xcode.app/Contents/MacOS/Xcode 2>&1 | tee /tmp/sk-log.txt

And then try to reproduce the issue and send them the log file (/tmp/sk-log.txt) so they can narrow it down and hopefully fix it in future Xcode versions.

然后尝试重现问题并将日志文件 ( /tmp/sk-log.txt)发送给他们,以便他们缩小范围并希望在未来的 Xcode 版本中修复它。

NoticeThis is project related issue and won't be fixed with OS updates, Xcode updates, or any number of restarting applications, at least the one that I'm having.

注意这是与项目相关的问题,不会通过操作系统更新、Xcode 更新或任何数量的重新启动应用程序来修复,至少是我拥有的应用程序。

回答by mnemonic23

I have Xcode's project created using Xcode8 long time ago. For some reason I have to upgrade to Xcode9 (9.4.1 exactly). Then I experienced that error only on that old project, not the new one created using Xcode9.

我很久以前使用 Xcode8 创建了 Xcode 的项目。出于某种原因,我必须升级到 Xcode9(准确地说是 9.4.1)。然后我只在那个旧项目上遇到了那个错误,而不是使用 Xcode9 创建的新项目。

So i think that error related to project issue. So I decided to compare the settings between old and new one. There are some differences, and after several tries, by changing Optimization Level for Debugsolved refactor issue.

所以我认为该错误与项目问题有关。所以我决定比较新旧设置之间的设置。有一些差异,经过多次尝试,通过更改Optimization Level for Debug解决了重构问题。

  1. In project editor, select your Target, Build Settings
  2. Set Optimization Level (Debug) = No optimization [-Onone]
  3. Delete DerivedDatafolder related to your project in /Users/YourMacUsername/Library/Developer/Xcode/DerivedData
  4. Clean project shiftcommandk.
  5. Build project commandb.
  1. 在项目编辑器中,选择您的Target,Build Settings
  2. Optimization Level (Debug) = No optimization [-Onone]
  3. 删除DerivedData与您的项目相关的文件夹/Users/YourMacUsername/Library/Developer/Xcode/DerivedData
  4. 清洁项目shiftcommandk
  5. 构建项目commandb

enter image description here

在此处输入图片说明

回答by WalterBeiter

I have the same issue in Xcode 10.3. Refactoring didn't work when I right clicked → refactor → rename on the class name in the class definition. However it did work when I did the same somewhere in code where I use that class.

我在 Xcode 10.3 中遇到了同样的问题。当我右键单击 → 重构 → 重命名类定义中的类名时,重构不起作用。但是,当我在使用该类的代码中的某处执行相同操作时,它确实起作用了。

回答by pableiros

I update to Xcode 10 and finally refactoring now it works again after a year without refactoring

我更新到 Xcode 10 并最终重构现在它在一年后再次运行而无需重构

回答by Richard Poutier

Product -> Clean Build Folder -> Quit Xcode -> Reopen Project -> Build

Product -> Clean Build Folder -> Quit Xcode -> Reopen Project -> Build

回答by Jose Antonio

Build your project (Command ?+ B) and it will fix the error. After doing it, I could rename my file successfully.

构建您的项目 ( Command ?+ B),它将修复错误。完成后,我可以成功重命名我的文件。

回答by user3001830

I was experiencing the exact same issue. It turned out that I had my build configuration set to Releasemode. Changing it to Debug, cleaning build folder and recompiling fixed the issue for me.

我遇到了完全相同的问题。结果是我的构建配置设置为Release模式。将其更改为Debug,清理构建文件夹并重新编译为我解决了该问题。