Xcode c++和Objective-C重构。

时间:2022-06-01 20:33:44

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

有没有一种方法可以在Xcode中重构混合c++ /Objective-C代码?我正在写一个游戏,使用Cocos2D和Box2D, Box2D是用c++写的,所以我写的每一个类都应该有。mm的扩展,所以当我尝试重命名这个变量时,我得到了一个类似“Xcode只能重构C和Objective-C代码”的信息。

Thanks in advance!

提前谢谢!

4 个解决方案

#1


10  

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.

对于重构,Xcode是非常有限的,即使是普通的objc。它基本上是重命名的,它甚至不能在注释中重命名。如果它说它不能做某件事,那它可能就做不到。

The only way to rename is using find & replace. Note that Xcode can handle regular expressions so it is often good enough.

重命名的唯一方法是使用查找和替换。注意,Xcode可以处理正则表达式,因此它通常足够好。

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.

您还可以使用命令行工具(例如sed)实现相同的功能。

#2


3  

I had the same problem in the cocos2d/box2d game I am building.

在我正在建造的cocos2d/box2d游戏中,我也遇到过同样的问题。

Fact 1: Xcode 4 can refactor C and Objective-C (.m files) code but it cannot refactor Objective-C++ code (.mm files).

事实1:Xcode 4可以重构C和Objective-C(。m文件)代码,但不能重构objective - c++代码(。mm文件)。

Fact 2: If you want to interact with box2d (which is written in c++) you need to use Objective-C++.

事实2:如果你想与box2d(用c++编写)交互,你需要使用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. 我在objective - c++中为我需要与之交互的box2d类创建了几个包装器(例如MyWorld)。mm是c++类b2World和MyBody的包装器。mm是c++类b2Body的包装器。这里的关键是避免在包装器的.h中引用box2d。
  3. 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.
  4. 在我的项目的每一个其他类别中,我从来没有直接提到过box2d类,我只提到了相关的包装器。当然,我也删除了#import“Box2D”。从这些类h”。
  5. I converted to plain Objective-C (.m) every other class of my project.
  6. 我将我的项目中的每一个其他类都转换为纯Objective-C (.m)。

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

结果是,现在Xcode可以重构我的项目的每一个类,当然也可以重构包装器。

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.

当然,当您需要在Objective-C类中使用包装器(例如MyWorld)时,您必须导入MyWorld。h和MyWorld.mm。

#3


2  

You might want to check out this project on github - it's a work in progress, but still:

你可能想看看github上的这个项目——这是一项正在进行的工作,但仍然:

https://github.com/lukhnos/refactorial

https://github.com/lukhnos/refactorial

It's a c++ refactoring tool based on Clang (i.e. the compiler Xcode itself is based on).

它是基于Clang(即编译器Xcode本身基于)的c++重构工具。

#4


0  

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:

我有一种解决方案——分离Objective-C和objective - c++代码。有一些宏允许检测在纯Objective-C中使用的文件。例子:

http://philjordan.eu/article/strategies-for-using-c++-in-objective-c-projects

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.

这个想法是,你仍然不能重构。mm文件,但是你可以重构所有其他的代码,即使它包括objective - c++代码。

#1


10  

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.

对于重构,Xcode是非常有限的,即使是普通的objc。它基本上是重命名的,它甚至不能在注释中重命名。如果它说它不能做某件事,那它可能就做不到。

The only way to rename is using find & replace. Note that Xcode can handle regular expressions so it is often good enough.

重命名的唯一方法是使用查找和替换。注意,Xcode可以处理正则表达式,因此它通常足够好。

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.

您还可以使用命令行工具(例如sed)实现相同的功能。

#2


3  

I had the same problem in the cocos2d/box2d game I am building.

在我正在建造的cocos2d/box2d游戏中,我也遇到过同样的问题。

Fact 1: Xcode 4 can refactor C and Objective-C (.m files) code but it cannot refactor Objective-C++ code (.mm files).

事实1:Xcode 4可以重构C和Objective-C(。m文件)代码,但不能重构objective - c++代码(。mm文件)。

Fact 2: If you want to interact with box2d (which is written in c++) you need to use Objective-C++.

事实2:如果你想与box2d(用c++编写)交互,你需要使用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. 我在objective - c++中为我需要与之交互的box2d类创建了几个包装器(例如MyWorld)。mm是c++类b2World和MyBody的包装器。mm是c++类b2Body的包装器。这里的关键是避免在包装器的.h中引用box2d。
  3. 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.
  4. 在我的项目的每一个其他类别中,我从来没有直接提到过box2d类,我只提到了相关的包装器。当然,我也删除了#import“Box2D”。从这些类h”。
  5. I converted to plain Objective-C (.m) every other class of my project.
  6. 我将我的项目中的每一个其他类都转换为纯Objective-C (.m)。

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

结果是,现在Xcode可以重构我的项目的每一个类,当然也可以重构包装器。

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.

当然,当您需要在Objective-C类中使用包装器(例如MyWorld)时,您必须导入MyWorld。h和MyWorld.mm。

#3


2  

You might want to check out this project on github - it's a work in progress, but still:

你可能想看看github上的这个项目——这是一项正在进行的工作,但仍然:

https://github.com/lukhnos/refactorial

https://github.com/lukhnos/refactorial

It's a c++ refactoring tool based on Clang (i.e. the compiler Xcode itself is based on).

它是基于Clang(即编译器Xcode本身基于)的c++重构工具。

#4


0  

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:

我有一种解决方案——分离Objective-C和objective - c++代码。有一些宏允许检测在纯Objective-C中使用的文件。例子:

http://philjordan.eu/article/strategies-for-using-c++-in-objective-c-projects

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.

这个想法是,你仍然不能重构。mm文件,但是你可以重构所有其他的代码,即使它包括objective - c++代码。