Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Rename classes in XCode projects Apps
Xcode is, for the most part, a fine IDE. But there are many things lacking. If you compare with Eclipse, for example, one of the big lacking aspects is refactoring.

Specific example: renaming a class is incredibly painful in Xcode. In Eclipse, you can rename a class and the file will be renamed, all the declarations will be modified, and all the uses of the class will be changed automatically. In Xcode, you have to do this by hand. This is even worse if you have used some of your classes in Interface Builder: some of the files it creates are binary, so things cannot be easily replaced there.

Read on for a partial workaround...

[robg adds: I can't test this one, lacking any real programming skills :) ... I also can't vouch for the problem itself...]

For Objective C and Xcode project files, it's easy to fix because they are all text files. Here's what I did:
  1. Open your project in Xcode and select "Clean all targets" in the Build menu.
  2. Quit Xcode.
  3. Make a full copy of your project directory, just in case.
  4. In a Terminal window, change into your project directory.
  5. For each class to rename, do this:
    mv OldClass.m NewClass.m
    mv OldClass.h NewClass.h
    perl -p -i.bak -e 's/OldClass/NewClass/g' `grep -r OldClass .`
    Note that the quotes surrounding the grep command are back ticks, not straight quotes.
A few comments:
  • The last command is doing textual substitution on all the files that contain the string OldClass. So make sure that the string does not appear somewhere you don't want to make the change. In particular, if the string is part of some other class or variable name that you do not want to rename, you will have problems.
  • The perl command will leave the old version of each file on a file with the same name plus the extension .bak. You can use the diff command to see what changed, to make sure nothing unexpected got changed. For example:
    diff ChangedFile.m.bak ChangedFile.m
    You could also use the FileMerge application (from /Developer/Applications/Utilities) to show the differences in a prettier format.
  • It will also replace the file name in the Xcode project file, so the next time you open your .xcode file, it should show up OK.
  • Beware of binary files! One of the files produced by Interface Builder, keyedobjects.nib, is binary, and if you have used any of the classes you want to rename (for example, to link them into your interface), their names will probably be there. In this case, you can use the technique described above only if the two class names are of the same length. If this is the case, things will work fine (I did this several times).
  • In any case, always make copies of the files before you modify them, and compare the files before and after each change, to look for unexpected substitutions.
That's it. It's quite pedestrian, and I'm sure there's a more intelligent way of substituting things in the IB keyedobjects.nib file, but if you are careful, it works quite well. Maybe Xcode 2.0 will have better refactoring support? I couldn't find any info on the XCode2 page, but who knows...
  • Currently 2.25 / 5
  You rated: 2 / 5 (4 votes cast)

Rename classes in XCode projects | 4 comments | Create New Account
Click here to return to the 'Rename classes in XCode projects' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Rename classes in XCode projects
Authored by: samsamoa on Mar 28, '05 01:18:44PM
There is a very nice CLI tool called nibtool (which comes with developer tools) that can do just this. From the manpage:
       -C, --convert old new
              Convert all custom classes in nibfile that have prefix old  into
              new.  Can  be used to convert all classes whose name begins with
     to now be prefixed by  or  all
              classes that have the prefix XY to now have the prefix XZ.
So to change OldClass to NewClass in MyNib.nib, do this:
    nibtool -C OldClass NewClass MyNib.nib

[ Reply to This | # ]
Rename classes in XCode projects
Authored by: jzdziarski on Apr 01, '05 10:28:44AM

It's much better to just do something like this:

perl -p -i.bak -e 's/OldClass/NewClass/g' `find . -name "*.java"`

or whatever extension you are interested in. this will prevent you from screwing with files you don't want..and grep -r never worked for me.

[ Reply to This | # ]
Rename classes in XCode projects
Authored by: ZZamboni on Apr 07, '05 02:32:13AM

I expected a tool for this would exist, but didn't find it when I needed it. Thanks!

[ Reply to This | # ]
Rename classes in XCode projects
Authored by: tiganu on Oct 26, '05 11:08:51AM
The grep line in the hint doesn't work very well, as it prints the line content along with the file name. This is better, and it avoids binary files
`grep -rIl OldClass .`

[ Reply to This | # ]