清理Objective-C代码 [英] Cleaning up Objective-C code

查看:96
本文介绍了清理Objective-C代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理复杂问题时,我发现自己尝试了各种解决方案,并且在尽力保持井井有条的同时,代码可能会变得非常混乱.对象可能会更改,不再使用,而其他时候,我可能会添加一些代码片段,这些代码片段最终不会被程序使用,但会占用空间并可能占用内存.

When working on complex problems, I find myself trying all sorts of solutions and, while doing my best to stay organized, the code can get quite messy. Objects may be changed and no longer be of use, while other times I may add snippets of code that do not end up being used by the program but are taking up space and possibly memory.

除了仔细阅读程序之外,还有其他方法可以找到程序未使用的代码块吗?

Aside from carefully reading through the program, are there ways of finding chunks of code that are not used by the program?

找到了一些清理程序的提示?

What are some tips you have found for cleaning up your program?

我发现要检查.h文件中的对象是否仍在应用程序中并检查它们是否已正确释放/释放,这是一个小技巧,它可以使用全部搜索"功能(cmd-shift-F)和按对象名称搜索

One small trick I found for checking that objects from the .h file are still used in the app and to check that they are properly deallocated/released is to use the "search all" function (cmd-shift-F) and search by the name of the object

推荐答案

以下是有关报告应用程序中代码覆盖率的几种方法的文章:

Here's an article about a few approaches to reporting code coverage in your application:

http://seriot.ch/blog.php?article=20080728

它面向Mac应用程序,但大多数情况下也适用于iPhone产品(DTrace,您只能在模拟器中使用)

It's oriented toward Mac applications but mostly applies to iPhone stuff too (DTrace you can only use in the simulator)

如文章所述,与其他语言相比,这在Objective-C中是一个更棘手的问题,因为使用performSelector调用方法非常容易,即使调用静态分析,静态分析也将报告为无效代码(是的.也可以在Java中执行类似的操作,但这种情况很少见.

As the article notes, this is a harder problem in objective-C than other languages since it's so easy to have a method that is called by performSelector, that static analysis would report as dead code even though it gets called (yes you can also do something similar in Javabut it's done far more rarely).

gcc警告标志可能是最好的主意,并仔细检查它认为是未调用的方法.实际上,在应用程序中运行每个可能的代码路径实际上都非常困难,但是如果您要删除的功能集较少(至少可以删除),则可以更快地做出选择,从而不必测试每个路径...

Probably the gcc warning flag is the best idea, along with careful examination of what it thinks are uncalled methods. Actually running every possible code path in an application is actually pretty hard, but if you have a smaller set of possibly functions to remove at least you can make choices more quickly so you wouldn't have to test every path...

我可能应该明确指出,代码覆盖率是一种您可以用来查找死"代码的技术,这正是您所追求的.

I should probably make clear that code coverage is a technique you can use to find "dead" code, which is what you were after

链接已死!我找不到缓存的版本,也记不起来足够概括它所包含的内容.

EDIT2 : Link is dead! I cannot find a cached version, and I can't remember it well enough to summarize more about what it contained.

这篇关于清理Objective-C代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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