Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to do refactoring on my Swift file in Xcode 9

Tags:

xcode

xcode9

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?

like image 728
CodeChanger Avatar asked Sep 27 '17 07:09

CodeChanger


People also ask

How do I refactor files in Xcode?

Select the lines of code you want to refactor, then Control-click and choose Refactor > Extract to Method. Xcode creates a new function, and highlights its name so you can rename it. If the lines of code reference parameters, Xcode includes those in the parameter list for the new function.

What is code refactoring in Swift?

August 22, 2017. Xi Ge. Xcode 9 includes a brand new refactoring engine. It can transform code locally within a single Swift source file, or globally, such as renaming a method or property that occurs in multiple files and even different languages.

How do I rename a file in Xcode?

Right-click on the class name in the interface ( . h ) file, choose Refactor->Rename , and Xcode will guide you through the process, allowing you to preview changes before you commit to them. Xcode will let you review the changes before you commit.


2 Answers

Workaround: Restart Xcode.

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

like image 197
Cannoliopsida Avatar answered Sep 19 '22 14:09

Cannoliopsida


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

like image 38
axumnemonic Avatar answered Sep 18 '22 14:09

axumnemonic