How to detect unused methods and #import in Objective-C

Viewed 45678

After working a long time on an iPhone app, I realized that my code is quite dirty, containing several #import and methods that are not called or useful at all.

I would like to know if there's any compiler directive or way to detect those useless lines of code. Does Xcode have any tool to detect this?

8 Answers

I recently wrote a script to find unused (or duplicate) #import statements: https://gist.github.com/Orangenhain/7691314

The script takes an ObjC .m file and starts commenting out each #import line in turn and seeing if the project still compiles. You will have to change BUILD_DIR & BUILD_CMD.

If you are using a find command to let the script run over multiple files, make sure to use a BUILD_CMD that actually uses all those files (or you will see a file with lots of unused import statements).

I wrote this without knowing AppCode has a similar feature, however when I tested AppCode it was not as thorough as this script (but way faster [for a whole project]).

U can use the clang-check tool to check single c base file. For example, commit one import line and run clang-check check the file, if no error then remove the import line.

Related