简体   繁体   中英

Xcode C++ and Objective-C refactoring

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 eg rename the variable, I got a message like " Xcode can only refactor C and Objective-C code ".

Thanks in advance!

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 (eg sed ) to achieve the same.

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:

  1. I created a few wrappers in Objective-C++ for the box2d classes I needed to interact with (eg MyWorld.mm is a wrapper of the c++ class b2World and MyBody.mm is a wrapper for the c++ class b2Body ). Here it is crucial to avoid any reference to box2d in the .h of the wrappers.
  2. In every other class of my project I never referred directly the box2d classes, I referred instead the related wrappers. Of course I also removed #import "Box2D.h" from these classes.
  3. I converted to plain Objective-C (.m) every other class of my project.

The result is that now Xcode can refactor every class of my project but the wrappers of course.

PS Of course when you need to use a wrapper (eg MyWorld) in you Objective-C classes you must import MyWorld.h and not MyWorld.mm .

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 (ie the compiler Xcode itself is based on).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM