iOS Analyze静态分析工具的使用

转自:https://www.jianshu.com/p/60d9afebfe30

Analyze主要分析以下四种问题:
1、逻辑错误:访问空指针或未初始化的变量等;
2、内存管理错误:如内存泄漏等;
3、声明错误:从未使用过的变量;
4、Api调用错误:未包含使用的库和框架。

1、Value stored to 'xxx' during its initialization is never read // 对象声明之后根本就没有使用 只有赋值

2、Value stored to 'xxx' is never read // 对象声明之后根本就没有使用 只有赋值

3、function call argument is an uninitialized value  // 所调用的方法没有初始化值

4、The 'viewWillAppear:' instance method in UIViewController subclass 'xxx' is missing a [super viewWillAppear:] call // 调用此类方法时需实现父类方法

5、Argument to 'NSMutableArray' method 'addObject:' cannot be nil // 被添加的对象不能为nil

一、简介

静态代码检测是白盒测试中很有效的发现代码问题的一种手段,通过一些工具的辅助,我们可以在进行详细的白盒测试前发现程序中一些潜在的问题。
Xcode Analyze 是苹果的开发工具 Xcode 自带的一个静态分析工具,功能强大且使用简单,主要会对代码中的几类错误加以标识:

  • **1. 逻辑缺陷:**访问空指针或未初始化的变量等;
  • **2. 内存管理错误:**如内存泄漏等;
  • 3. 无用存储逻辑:(永远不会被访问的变量、永远不会执行的代码);
  • **4. Api调用错误:**未包含使用的库和框架;

官方简介:
Code may have subtle errors that slip by the compiler and manifest themselves only at runtime, when they could be difficult to identify and fix. The static analyzer parses your code and identifies these types of problems:

  • Logic flaws, such as accessing uninitialized variables and dereferencing null pointers
  • Memory management flaws, such as leaking allocated memory
  • Dead store (unused variable) flaws
  • API-usage flaws that result from not following the policies required by the frameworks and libraries the project is using
    详情可见:
    苹果Analyze官方文档——Edite source code——Analyze your code

二、使用方法和常见问题举例

1、使用方法

运行 XcodeAnalyze 的步骤是,Product ----> Analyze 。(快捷键是command + shift + B)

image

image

我的项目分析出了262个问题。
下面摘用了简书上一位大神会飞的猪X的图片

image

这个工具会把错误进行分类,每个分类下可以查看到具体的代码行。

2、常见问题举例

(1)Localizability Issue (Apple)

image

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-h8hiwT4z-1598585244094)(//upload-images.jianshu.io/upload_images/1802326-99674406027a68d1.png?imageMogr2/auto-orient/strip|imageView2/2/w/1140)]

意思是:面向用户的文本应使用本地化的字符串宏。
我的项目中错误最多的就是这个,因为很多地方的字符串没有做本地化处理或者宏定义。
解决办法:
1、在项目中添加本地化。
2、或者在 Build Settings 里面搜索**“localizability”**找到 Missing Localizability(缺少本地化) 设置为 NO,就会忽略这个问题。如下图:

image

(2)Core Foundation/Objective-C

image

image

**意思是:**我这里漏写了 [super viewDidLoad];
解决办法: 补上 [super viewDidLoad];

(3)Logic error

image

image

意思是:这个对象从来没有被读取使用过。
解决办法: 删掉永远不会被使用的对象声明,或者查找问题是不是该使用但是没有使用。

(4)Memory error

image

image

“nil returned from a method that is expected to return a non-null value”
意思是:不能传空却有可能传了空值。
解决办法: 我这里是因为只写了else if 的判断却没有 else 的判断,所以有一定几率返回的cell是空的,把else if 改成 else 就好了。其他情景也类似,只要不该空的时候不传空就行。

(5)Memory (Core Foundation/Objective-C)

image

image

“Potential leak of an object stored into ‘nextBuffer’”
意思是:nextBuffer这个对象存在内存泄漏。
解决办法: 检查是否存在内存泄漏的可能。

(6)Dead store

image

image

“Value stored to ‘replyString’ during its initialization is never read”
意思是:这个对象从来没有被读取使用过。
解决办法: 删掉永远不会被使用的对象声明,或者查找问题是不是该使用但是没有使用。

除了静态分析工具Analyze,还有动态分析Instruments,之后我也会写篇文章学习并分享。谢谢大家。

以上总结参考了并部分摘抄了以下文章,非常感谢以下作者的分享!:
1、作者会飞的猪X的《Xcode之Analyze静态分析》
2、作者NY的 《【iOS测试系列】静态代码分析工具Xcode Analyze+OCLint》
3、作者船长_的《Xcode执行Analyze静态分析》
4、作者朕君无戏言的《iOS开发_性能优化(Analyze和Instruments的使用)》
5、苹果Analyze官方文档

猜你喜欢

转载自blog.csdn.net/iotjin/article/details/108276379