Using Xcode 4 Refactor to rename a class

I had reason recently to want to rename a class within a project. Doing that manually is a pain if you consider all of the places that a class name can be referenced:

Luckily Xcode can take care of all the above for you. The trick is to open the interface file of the class to be renamed, select the class name in the @interface declaration and then from the Xcode Edit menu select Refactor -> Rename…

Xcode then shows a dialog box prompting you for the new name of the class. You should make sure to also select the “Rename related files” checkbox to have Xcode also rename the .h and .m files and fix anywhere that imports the .h file:

At this point Xcode displays all of the files impacted by the rename operation. You can use this to review the changes with Xcode presenting a diff of each file with the old and new versions of the version displayed side-by-side:

Once you have reviewed all of the changes and selected “Save” Xcode provides you a last chance to take a snapshot of the project. Since I am using Git to manage my source code I skip this step:

That is all there is to it. Not something I need to do everyday but it is one of those things that when you need it you need it bad.

Never miss a post!

iOS Size Classes Cheat Sheet

Subscribe and get my free iOS Size Classes Cheat Sheet

Success! Now check your email to confirm your subscription and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
No time to watch WWDC videos?

Sign up to get my iOS posts direct to your inbox and I will send you a free PDF of my iOS Size Classes Cheat Sheet.

OK! Check your inbox (or spam folder) for an email to confirm your details and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
Archives Categories