参考资源链接:
Static Analysis on iOS - Part I
Static Analysis on iOS - Part II
Xcode执行静态代码分析(这位大神就是根据官方文档写的博客)
设置工程自动进行静态分析
选中工程文件,在TARGETS的Build Settings选项中的搜索栏中搜索关键字,analyze during 'Build',在结果中将Analyze During 'Build'选项修改为YES。
Xcode 7.2注:描述于IOS9.2下
Performing Static Code Analysis
Find flaws—potential bugs—in the source code of a project with the static analyzer built into Xcode. Source 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.
1.Choose Product > Analyze.
2.In the issue navigator, select an analyzer message.
3.In the source editor, click the corresponding message.
4.Use the pop-up menu in the analysis results bar above the edit area to study the flow path of the flaw.
5.Edit the code to fix the flaw.
The video shows the process of looking at a flaw in the source file SKTText.m.
The Xcode static analyzer parses the project source code and identifies these types of problems:
1.Logic flaws, such as accessing uninitialized variables and dereferencing null pointers
2.Memory management flaws, such as leaking allocated memory
3.Dead store (unused variable) flaws
4.API-usage flaws that result from not following the policies required by the frameworks and libraries the project is using
You can suppress false positive messages from the analyzer using assertions, attributes, or pragma directives.
When you analyze a project for the first time, you may uncover a lot of issues. But if you run the static analyzer regularly and fix the flaws it uncovers, you should see fewer problems in subsequent analyses. Analyze early; analyze often. It’s good for the code.
Note that if the static analyzer reports no problems, you can't assume that there are none. The tool cannot necessarily detect all the flaws in the source code
网友评论