46

Is there a way to refactor swift in Xcode 7.1?

When I try to rename a variable (e.g. highlight, right-click, refactor > rename), Xcode pops up an error dialog saying:

Can't refactor Swift code. Xcode can only refactor C and Objective-C code.

Is there a workaround for this?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Paul Van Wieren
  • 985
  • 1
  • 7
  • 11
  • 5
    At present, you can't. Similar question here: http://stackoverflow.com/questions/24133664/xcode-can-only-refactor-c-and-objective-c-code-how-to-rename-swift-class-name-i. – Martin R Nov 03 '15 at 11:09
  • 33
    It is beyond insane that in 2016, a major IDE cannot handle such a basic task as refactoring. In 1995 with Visual Basic 3 I was able to refactor. – MusiGenesis Jan 21 '16 at 21:01
  • 5
    Same issue continues to Xcode 8. :( – Shad Sep 23 '16 at 19:59
  • Why Apple won't improve on this? Honestly Xcode is far behind the other major IDE already. – Bagusflyer Oct 25 '16 at 13:29
  • Xcode 9 promises the refactoring of Swift. Release note of Xcode 9: https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/WhatsNewXcode/xcode_9/xcode_9.html#//apple_ref/doc/uid/TP40004626-CH8-SW2 – Shad Jun 07 '17 at 03:55
  • @Shad it was about time :) – Dániel Nagy Jun 14 '17 at 14:13
  • Please update the accepted answer – pkamb Oct 15 '20 at 19:34

4 Answers4

86

AFAIK the refactoring is not working with Swift right now, however, at least in the same file(scope), you can do the following: enter image description here

Dániel Nagy
  • 11,815
  • 9
  • 50
  • 58
14

Xcode 9 - 10

Xcode 9 now supports refactoring in Swift. This was announced in WWDC 2017.

enter image description here

Guy Daher
  • 5,526
  • 5
  • 42
  • 67
6

The lack of refactoring is astounding (especially as I am coming off of a major stint using VS) However I have just noticed that someone has done something about it. On GitHub there is Refactorator Xcode Plugin (MIT license) which claims:

Refactorator is an Xcode plugin for refactoring Swift & now Objective-C code. It will rename public or internal vars, functions, enums etc. For private and local entities use Xcode's existing "Edit All in Scope" functionality.

Full disclosure is that I have nothing to do with them and am just about to install the product myself.

Peter M
  • 7,309
  • 3
  • 50
  • 91
  • I’ve had no luck with it. For example to rename a variable it may pick up most, but not all usages (why?) - find and replace gets 100%. I’ve not been impressed. – Lloyd Sargent Aug 23 '16 at 22:01
2

As of Xcode 7.2 beta the refactoring tools do not support Swift.

What I do is use the type checker. Change the name/type of something and then fix all the compile breakages. Because of the strong typing you can have more confidence that this approach will catch everything. I have at times deliberately changed things and broken them temporarily to check where objects or types are used or accessed from.

Or use AppCode for the refactoring, I hear it is good but haven't tried much myself.

Joseph Lord
  • 6,446
  • 1
  • 28
  • 32