Reputation: 8351
I am using Xcode 9 and I am trying to do Refactoring on my Swift
based file but every time I am getting below error:
Refactoring engine ranges didn't match initial ranges
Why isn't it matching the initial range?
Upvotes: 55
Views: 15756
Reputation: 3028
None of the below worked for me. But I could do it. This is my solution.
My case was like:
variable = [self functionName];
and "functionName" failed to refactor > rename. What I did is took this function call in brackets like this:
variable = ([self functionName]);
After that I could rename this function.
Upvotes: 0
Reputation: 1
In case none of the above works (usually does for me too, except for this one time when it didn't), this is what worked for me:
Note: I'm using Xcode 14.3
Upvotes: 0
Reputation: 61
Xcode 14.2: New project and tried to refactor the name of the ViewController and got the rename error pop up. Saved the the project and did a build. Refactoring the file name worked after that.
Upvotes: 0
Reputation: 63
I experienced this on a Mac M1, using Xcode Version 14.1 (14B47b).
This helped: Clean project: shift-command-k. Build project: command-b.
Upvotes: 1
Reputation: 1691
DerivedData
This worked for me for this error and other kind of refactoring errors.
rm -rf ~/Library/Developer/Xcode/DerivedData
Upvotes: 0
Reputation: 4882
I am on Mac M1 and using Xcode Version 12.5 beta 3.
I was facing the same issue when trying to rename ViewController.
Before renaming it, I moved it to a new group, and my code was in a running state. Not sure if that was the reason for the issue.
But I restarted the Xcode and the issue was resolved for me.
Upvotes: 2
Reputation: 3257
Workaround: Restart Xcode.
This has not been resolved yet as of January 2018 (Xcode 9.2).
Upvotes: 30
Reputation: 217
Product -> Clean Build Folder -> Quit Xcode -> Reopen Project -> Build
Upvotes: 3
Reputation: 506
Build your project (Command ⌘ + B) and it will fix the error. After doing it, I could rename my file successfully.
Upvotes: 38
Reputation: 2354
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.
Upvotes: 4
Reputation: 16032
I update to Xcode 10 and finally refactoring now it works again after a year without refactoring
Upvotes: 2
Reputation: 5103
Update For 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:
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.
Notice This 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.
Upvotes: 7
Reputation: 2316
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.
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 Debug
solved refactor issue.
Target
, Build Settings
Optimization Level (Debug) = No optimization [-Onone]
DerivedData
folder related to your project in /Users/YourMacUsername/Library/Developer/Xcode/DerivedData
Upvotes: 7
Reputation: 1
I was experiencing the exact same issue. It turned out that I had my build configuration set to Release
mode. Changing it to Debug
, cleaning build folder and recompiling fixed the issue for me.
Upvotes: 0