Xcode C++ 和 Objective-C 重构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15839664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:08:31  来源:igfitidea点击:

Xcode C++ and Objective-C refactoring

c++objective-cxcodecocos2d-iphonebox2d

提问by Terko

Is there a way to refactormixed 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!

提前致谢!

回答by Sulthan

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 的重构非常有限,即使是普通的 Obj-C。它基本上是重命名,甚至不能在评论中重命名。如果它说它不能做某事,那么它可能不能。

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

重命名的唯一方法是使用find & replace. 请注意,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.

当然,问题是 find & replace 不了解编程语言,有时会添加一些额外的替换或忘记替换某些内容,因此要格外小心。每次重构后都需要清理构建以检查所有内容是否正确重命名。

You can also use command line tools (e.g. sed) to achieve the same.

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

回答by Luca Angeletti

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.mmis a wrapper of the c++ class b2Worldand MyBody.mmis 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.
  1. 我在 Objective-C++ 中为我需要与之交互的box2d类创建了一些包装器(例如MyWorld.mm是 c++ 类b2World的包装器,而MyBody.mm是 c++ 类b2Body的包装器)。在这里,避免在包装器的 .h 中对 box2d 的任何引用至关重要。
  2. 在我项目的每个其他类中,我从未直接引用 box2d 类,而是引用了相关的包装器。当然,我也从这些类中删除了#import "Box2D.h"
  3. 我将项目的每个其他类都转换为普通的 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.hand not MyWorld.mm.

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

回答by cacau

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 的 C++ 重构工具(即 Xcode 本身所基于的编译器)。

回答by Vyachaslav Gerchicov

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++ 代码。