如何在Objective-C中检测未使用的方法和#import [英] How to detect unused methods and #import in Objective-C

查看:113
本文介绍了如何在Objective-C中检测未使用的方法和#import的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iPhone应用程序上工作了很长时间之后,我意识到我的代码很脏,其中包含一些#import和根本没有被调用或有用的方法.

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.

我想知道是否有任何编译器指令或方法来检测那些无用的代码行. Xcode是否有任何工具可以检测到这一点?

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?

推荐答案

Xcode允许您(取消)检查特定编译器警告的设置,这些警告可以警告您某些类型的未使用代码. (在源列表中选择项目,然后选择文件">获取信息",然后选择构建"选项卡.)以下是一些您可能感兴趣的内容(对我来说,这对Clang和GCC 4.2来说是这样):

Xcode allows you to (un)check settings for specific compiler warnings that can warn you of some types of unused code. (Select the project in the source list and File > Get Info, then select the Build tab.) Here are a few (which show up for Clang and GCC 4.2 for me) which may be of interest:

  • 未使用的功能
  • 未使用的参数
  • 未使用的值

我没有发现用于检测未使用的导入的任何选项,但这有点简单-技术含量低的方法只是注释掉导入语句,直到出现编译错误/警告为止.

I don't see any options for detecting unused imports, but that is a bit simpler — the low-tech approach is just to comment out import statements until you get a compile error/warning.

未使用的Objective-C方法比未使用的C函数更难检测,因为消息是动态调度的.警告或错误可以告诉您您有潜在的问题,但是缺少警告或错误并不能保证您不会遇到运行时错误.

Unused Objective-C methods are much more difficult to detect than unused C functions because messages are dispatched dynamically. A warning or error can tell you that you have a potential problem, but the lack of one doesn't guarantee you won't have runtime errors.

编辑:另一种检测(潜在)未使用方法的好方法是从实际执行中检查代码覆盖率.通常,这是与自动化单元测试相结合完成的,但并非必须如此.

Another good way to detect (potentially) unused methods is to examine code coverage from actual executions. This is usually done in tandem with automated unit testing, but doesn't have to be.

此博客post 是使用Xcode进行单元测试和代码覆盖的不错介绍. gcov 上的部分(仅适用于GCC生成的代码)说明了如何使Xcode生成可记录执行频率的检测代码.如果您在模拟器中使用经过测试的应用构建版本,然后在其上运行gcov,则可以使用 lcov (用于创建HTML报告的Perl脚本).

This blog post is a decent introduction to unit testing and code coverage using Xcode. The section on gcov (which only works with code generated by GCC, by the way) explains how to get Xcode to build instrumented code that can record how often it has been executed. If you take an instrumented build of your app for a spin in the simulator, then run gcov on it, you can see what code was executed by using a tool like CoverStory (a fairly simplistic GUI) or lcov (Perl scripts to create HTML reports).

我将gcovlcov用于 CHDataStructures.framework 并自动生成每次提交SVN后,覆盖率报告.再次提醒您,将执行覆盖率作为确定哪些代码死机"的绝对度量是不明智的,但是它肯定可以帮助您确定可以进一步研究的方法.

I use gcov and lcov for CHDataStructures.framework and auto-generate coverage reports after each SVN commit. Again, remember that it's unwise to treat executed coverage as a definitive measure of what code is "dead", but it can certainly help identify methods that you can investigate further.

最后,由于您正在尝试删除无效的代码,因此我认为您也会发现这个有趣的问题:

Lastly, since you're trying to remove dead code, I think you'll find this SO question interesting as well:

这篇关于如何在Objective-C中检测未使用的方法和#import的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆