Rename a class in Xcode: Refactor... is grayed out (disabled). Why?

IphoneXcodeRefactoring

Iphone Problem Overview


Why is Refactor... grayed out (disabled) in Xcode?

I'd like to rename a class.

Iphone Solutions


Solution 1 - Iphone

Select the class's symbol in its header file - i.e. the bit just after @interface. Then the refactoring stuff should be enabled.

Solution 2 - Iphone

If you are using Objective-C++ (i.e. mix Objective-C with C++ code) then refactoring is disabled in xcode since it does not support refactoring of C++ code.

Solution 3 - Iphone

Refactor might also be disabled if affected files (most likely the file with your class in it) are not saved.

Solution 4 - Iphone

I've been using Xcode for 5 years now, and refactoring has never worked correctly (even xcode 4.6 has major bugs where it WILL corrupt your source code!).

The workaround has always been (still works 100%, even in cases where Apple's code fails)

  1. use shift-command-f to find all uses of the file
  2. select "replace" in the search settings
  3. "replace-all"
  4. do the following for the .h file, and REPEAT IT FOR THE .m FILE (if you have one):
    1. right click the original file, and select "show in finder"
    2. delete the file from xcode (select "delete references only" when asked)
  5. rename the .h (and .m if you have one) in Finder
  6. in Xcode, select "Product -> (hold down Alt) -> Clean Build Folder"
  7. quit xcode (you can usually get away with not doing this - but NOTE: there are some other MAJOR bugs in Xcode where it crashes itself if you don't do this)
  8. re-open xcode
  9. drag/drop the .h and .m back into Xcode
  10. wait a few seconds (some of Xcode's core methods are asynchronous - allowing it to corrupt your project)
  11. finally, when it seems to be doing nothing (and your hard disk isn't making any noise any more): cmd-b to re-build

I have a 100% success rate with this method. I just tried refactoring with Apple's "Refactor -> Rename" in latest xcode and it failed - again!

(this time with the incorrect error: "Unable to determine the language of", one of those error messages where Apple put the wrong text in place)

Solution 5 - Iphone

I'm going to my project at finder, then change files name by get info.

After that, at xcode -> Project Navigator I delete the files.

At end, I click right on the class and Add files to ..., and add these files again.

It worked for me.

Solution 6 - Iphone

For me I realized Refactor was disabled because the Xcode project I had opened was referencing a Base SDK that was missing. Edit Project Settings and in the Build tab set the Base SDK to one that you have (like for me this was iOS 4.2). This enabled Refactor for me.

Solution 7 - Iphone

Also, it could happen that you renamed the filename for the class, either outside xcode or by ctrl-clicking the filename and then renaming it. xcode refuses to refactor if filename does not match with the class name.

Solution 8 - Iphone

Go to your class' header file and find the line that looks similar to this:

@interface YourClassName

Right click on the class symbol (e.g. YourClassName) and you should be able to select Refactor -> Rename...

I just tried this and it works in Xcode 5.

Solution 9 - Iphone

This may be a bit late, but I stumbled across this post because I was unable to refactor my "ViewController.swift" file to "WhateverViewController.swift". I tried selecting the file in the Project Navigator and then selecting "Editor -> Refactor" from the top menu, but 'rename' is always greyed out.

Instead, what worked was selecting the ViewController name from the editor. So if you have:

class ViewController: UIViewController {
 // Code here...
}

Highlight the "ViewController" word and then select Refactor from the menu or right-click and select Refactor -> rename.

Hopefully that helps...

Solution 10 - Iphone

Had this problem as well. I ran through trying to find missing SDKs, saving files, and looking for Objective-C++ code as mentioned above, and all it took to fix my machine was rebooting XCode.

Seems a little buggy still.

BTW, this was for XCode 4.0.1

Solution 11 - Iphone

Refactoring works If you first change the file name in the project navigator.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElliotView Question on Stackoverflow
Solution 1 - IphoneNoah WitherspoonView Answer on Stackoverflow
Solution 2 - IphoneVolker VoeckingView Answer on Stackoverflow
Solution 3 - IphoneklaaspieterView Answer on Stackoverflow
Solution 4 - IphoneAdamView Answer on Stackoverflow
Solution 5 - IphoneoffsetView Answer on Stackoverflow
Solution 6 - Iphoneuser591345View Answer on Stackoverflow
Solution 7 - Iphonecarlos_msView Answer on Stackoverflow
Solution 8 - IphoneeasytargetView Answer on Stackoverflow
Solution 9 - IphoneMichaelView Answer on Stackoverflow
Solution 10 - IphonegdavisView Answer on Stackoverflow
Solution 11 - IphoneChrisView Answer on Stackoverflow