Fabian
Fabian

Reputation: 512

Xcode 9.1 Refactoring not available

The refactoring menu in Xcode 9.1 is completely greyed out. I wanted to extract a method from existing swift code and got the following screen: enter image description here

Do I have to enabled something special to get access to the new refactoring features?

Upvotes: 8

Views: 3367

Answers (5)

AE0089
AE0089

Reputation: 31

Small thing for me was that the code I was trying to access was inside a closure so when I was selecting it and right clicking to Extract to a Method, it was greyed out. removing self on both instances, I could then select both lines to then extract to method

self?.activityIndicator.stopAnimating()
self?.activityIndicator.removeFromSuperview()

Both lines were inside a closure for a UIAlertAction. So removing self allows you to extract to a method in this case. Then just call self on the extracted method inside the closure and it should work.

self?.hideSpinner() //extracted method name

As other posters have already said, its best to make sure you're able to refactor what you want by selecting the right code.

Upvotes: 0

PJSimon
PJSimon

Reputation: 343

I had to change to the Active Schema to the one that matched the Target Membership of the file I was editing.

In other words:

  • In Project Navigator, select the file you're editing
  • Show the Inspectors panel on the right
  • Show the File Inspector
  • Observe the Target Membership values and set the Active Schema (at the top, by the Build and Stop buttons) to match.
  • Select text, right click, select Refactor
  • Observe all the Refactor options are now enabled.

Upvotes: 2

Rajneesh071
Rajneesh071

Reputation: 31081

Refactor option hides if you select version editor.
Thanks @paul

Upvotes: 1

Sachi Perez
Sachi Perez

Reputation: 9

This happened to me too and I could not find anything on it so...

My first post is born.

Mike T. hit on a small portion above (incomplete syntax).

Nothing is wrong. Xcode is amazing! It seems the Refactor/Extract feature is pretty smart. The option is not available if it does not make sense to extract a method.

It may not make sense to extract a method for many reasons. Xcode disables the feature when it encounters them in the selection. A few that I noticed are:

  • variable declarations
  • a solitary print statement
  • continue statements in the selection but I don't include the for loop container. (i'm impressed)

my guess is that there are a bunch of conditions...

Upvotes: 0

Deep Parekh
Deep Parekh

Reputation: 445

Try cleaning your Xcode. Also try clearing out derived data and restart Xcode.

Clean Build --> (Command-Option-Shift-K)
Delete DerivedData folder in ~/Library/Developer/Xcode/DerivedData

Upvotes: 6

Related Questions