Having trouble refactoring

I'm having trouble refactoring a class. I've tried everything under the sun that has been suggested on various other forums (clear derived data and re-build as well as re-copying project folder) but I can't seem to get the error message to go away and refactor successfully. I have tried highlighting the class name in the .h/.m files, then refactoring, and it does not work like it previously had. Any ideas how to resolve the issue?


I've tried on Xcode 7.1 (7B91b) as well as 7.2 beta (7C46l). My project is 100% Obj-c.


Error:

The selection is not a type that can be renamed.

Make a different selection and try again.


Answered by falco in 86990022

Yes, refactoring is badly broken in Xcode 7.
I have found similar problems when trying to rename properties, methods and classes. Yesterday I downloaded 7.1.1 with a hope that there might be some improvements but none so far. I'll file radar today, but in the meantime below is what I've found after some testing.
I did some testing in a sample single view project in Objective-C on Xcode 7.1.1 and found the following:

  1. If you try to rename a property that has IBOutlet keyword, it will fail;
  2. If you try to rename another property which is not IBOutlet, but above it's declaration you have any property that is IBOutlet, rename will fail;
  3. If you try to rename a method which is declared below IBOutlet property and do it right clicking the method in .h file, rename will fail;
  4. If you try to rename a class that #imports a class that has some IBOutlet declarations in the header file, rename will fail (highlighted since this is the case, that original question is about)


Luckily I also found some workarounds:

  1. If you move the declaration of your property above IBOutlet declarations, rename will succeed;
  2. If you need to rename IBOutlet - you can move it above all other IBOutlets and remove the IBOutlet keyword of it before renaming it. Rename will succeed (and will also rename it within Storyboard), then you can add the IBOutlet keyword back.
  3. If you move declaration of your method above any IBOutlet properties rename will succeed from .h file, but alternatively it also seems to work if you try to rename it when selecting in a .m file (the latter case works even if method is declared in .h file below IBoutlet properties).
  4. When renaming class that imports another class with IBOutlets, you can do the following - move the IBOutlets of the other class from .h file to .m file. Then rename works. Alternatively - remove all IBOutlet keywords before the rename, and then add them back after it.


Note: When I say that rename succeeds, I mean - it won't show you "The selection is not a type that can be renamed". However it still does not work smoothly most of the time:


  1. It does not show the changes to be made in files, like it used to do
  2. It may complain about the fact that files have been modified by another application while Xcode has unsaved changes. As I have found, usually you need to choose "Keep Xcode Version"
  3. It may fail to rename the property in all of the files it has been used. This is the most important, which in some cases make all the workarounds useless.


Hope it helps 🙂

Anybody have a solution for this?

Accepted Answer

Yes, refactoring is badly broken in Xcode 7.
I have found similar problems when trying to rename properties, methods and classes. Yesterday I downloaded 7.1.1 with a hope that there might be some improvements but none so far. I'll file radar today, but in the meantime below is what I've found after some testing.
I did some testing in a sample single view project in Objective-C on Xcode 7.1.1 and found the following:

  1. If you try to rename a property that has IBOutlet keyword, it will fail;
  2. If you try to rename another property which is not IBOutlet, but above it's declaration you have any property that is IBOutlet, rename will fail;
  3. If you try to rename a method which is declared below IBOutlet property and do it right clicking the method in .h file, rename will fail;
  4. If you try to rename a class that #imports a class that has some IBOutlet declarations in the header file, rename will fail (highlighted since this is the case, that original question is about)


Luckily I also found some workarounds:

  1. If you move the declaration of your property above IBOutlet declarations, rename will succeed;
  2. If you need to rename IBOutlet - you can move it above all other IBOutlets and remove the IBOutlet keyword of it before renaming it. Rename will succeed (and will also rename it within Storyboard), then you can add the IBOutlet keyword back.
  3. If you move declaration of your method above any IBOutlet properties rename will succeed from .h file, but alternatively it also seems to work if you try to rename it when selecting in a .m file (the latter case works even if method is declared in .h file below IBoutlet properties).
  4. When renaming class that imports another class with IBOutlets, you can do the following - move the IBOutlets of the other class from .h file to .m file. Then rename works. Alternatively - remove all IBOutlet keywords before the rename, and then add them back after it.


Note: When I say that rename succeeds, I mean - it won't show you "The selection is not a type that can be renamed". However it still does not work smoothly most of the time:


  1. It does not show the changes to be made in files, like it used to do
  2. It may complain about the fact that files have been modified by another application while Xcode has unsaved changes. As I have found, usually you need to choose "Keep Xcode Version"
  3. It may fail to rename the property in all of the files it has been used. This is the most important, which in some cases make all the workarounds useless.


Hope it helps 🙂

5 years have passed, but the problem is still relevant (Xcode 12.4).
As a temporary solution, Xcode has to be closed completely. After that, the project is re-indexed and there is enough memory to rename 2-3 classes, after which the action has to be repeated.
Having trouble refactoring
 
 
Q