Rename classes in XCode projects

Mar 28, '05 09:27:00AM

Contributed by: ZZamboni

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: 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...

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050326112911729