CodeChanger
CodeChanger

Reputation: 8351

Unable to do refactoring on my Swift file in Xcode 9

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

enter image description here

Why isn't it matching the initial range?

Upvotes: 55

Views: 15756

Answers (14)

Kibernetik
Kibernetik

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

Tamara Andonovska
Tamara Andonovska

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:

  1. I remove reference of the file (but still keep the actual file). Remove reference window option
  2. I open the folder where I have the file I previously removed and I drag and drop the file in the same place I needed it to be. Make sure Copy items if needed in unselected. Drag-and-drop window option
  3. Clean build folder and Build.
  4. Wait for indexing to finish.
  5. Refactor.

Note: I'm using Xcode 14.3

Upvotes: 0

Bob Taylor
Bob Taylor

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

Saladel
Saladel

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

Pedro Trujillo
Pedro Trujillo

Reputation: 1691

Remove DerivedData

This worked for me for this error and other kind of refactoring errors.

  1. Close Xcode
  2. Remove DerivedData:

rm -rf ~/Library/Developer/Xcode/DerivedData

Upvotes: 0

Amit Baderia
Amit Baderia

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

The Beruriah Incident
The Beruriah Incident

Reputation: 3257

Workaround: Restart Xcode.

This has not been resolved yet as of January 2018 (Xcode 9.2).

Upvotes: 30

Richard Poutier
Richard Poutier

Reputation: 217

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

Upvotes: 3

Jose Antonio
Jose Antonio

Reputation: 506

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

Upvotes: 38

WalterBeiter
WalterBeiter

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

pableiros
pableiros

Reputation: 16032

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

Upvotes: 2

manman
manman

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

axunic
axunic

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.

  1. In project editor, select your Target, Build Settings
  2. Set Optimization Level (Debug) = No optimization [-Onone]
  3. Delete DerivedData folder related to your project in /Users/YourMacUsername/Library/Developer/Xcode/DerivedData
  4. Clean project shiftcommandk.
  5. Build project commandb.

enter image description here

Upvotes: 7

user3001830
user3001830

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

Related Questions