Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Thanks in advance!

like image 531
Terko Avatar asked Apr 05 '13 17:04

Terko


People also ask

What is refactoring in Xcode?

Refactor code into functionsWhen you have code that repeats in a function or code that you can reuse, refactor it into a function. Select the lines of code you want to refactor, then Control-click and choose Refactor > Extract to Method. Xcode creates a new function, and highlights its name so you can rename it.

How do I refactor code in Swift?

Renaming Swift codeThe renaming refactoring option can be selected from the refactoring menu by right-clicking the piece of Swift code you want to rename. It will open a new edit overview which will show all the references which will be renamed across multiple files.

How do I open an Objective C project in Xcode?

Starting XcodeClick on the Xcode icon in the dock to launch the tool. Click Next and on the resulting options panel name the project sampleApp and select Foundation from the Type menu. Also verify that the Use Automatic Reference Counting option is selected.

How do I search inside Xcode?

Use cmd + shift + f for complete Xcode search.


1 Answers

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.

like image 105
Sulthan Avatar answered Oct 14 '22 08:10

Sulthan