Xcode C++ and Objective-C refactoring

假如想象 提交于 2019-12-07 00:40:45

问题


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!


回答1:


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.




回答2:


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 (e.g. 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.

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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/15839664/xcode-c-and-objective-c-refactoring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!