Is there a way to refactor mixed C++/Objective-C code in Xcode ?? I am writing a game using Cocos2D and Box2D, and the Box2D is written on C++, so every class I write should have .mm extension and therefore when I try to e.g. rename the variable, I got a message like "Xcode can only refactor C and Objective-C code".
Thanks in advance!
You might want to check out this project on github - it's a work in progress, but still:
https://github.com/lukhnos/refactorial
It's a c++ refactoring tool based on Clang (i.e. the compiler Xcode itself is based on).
I had the same problem in the cocos2d/box2d game I am building.
Fact 1: Xcode 4 can refactor C and Objective-C (.m files) code but it cannot refactor Objective-C++ code (.mm files).
Fact 2: If you want to interact with box2d (which is written in c++) you need to use Objective-C++.
I have (partially) addressed the problem following these steps:
The result is that now Xcode can refactor every class of my project but the wrappers of course.
P.S. Of course when you need to use a wrapper (e.g. MyWorld) in you Objective-C classes you must import MyWorld.h and not MyWorld.mm.
I had some kind of a solution - to separate Objective-C and Objective-C++ code. There are some macros which allow to detect if the file used in pure Objective-C. Example:
http://philjordan.eu/article/strategies-for-using-c++-in-objective-c-projects
The idea is you still can't refactor .mm files but you can refactor all the other code even if it includes Objective-C++ code.
Xcode is VERY limited with refactoring, even with plain Obj-C. It's basically renaming and it can't even rename in comments. If it says it can't do something, then it probably can't.
The only way to rename is using
find & replace
. Note that Xcode can handle regular expressions so it is often good enough.Of course, the problem is that find & replace doesn't know the programming language and sometimes can add some extra replace or forget to replace something, therefore be extra careful. Clean build is neccessary after every refactoring to check everything got renamed correctly.
You can also use command line tools (e.g.
sed
) to achieve the same.