Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
This chapter is from the book

Meet the Static Analyzer

One of the handiest tools in Xcode is the static analyzer. The static analyzer uses Apple’s LLVM compiler technology to analyze your code and find bugs. Traditionally, developers have relied on compiler warnings for hints on potential trouble areas in their code. The static analyzer goes much deeper, looking past syntax and tracing how values are used within your code.

Because of the default compiler settings and our careful typing, you should find, if you run the analyzer now, that our application has no issues as it stands. Let’s modify our project settings so that we can better see the static analyzer at work.

As we did before, open the project’s build settings by selecting the project in the project navigator on the left. Then select the lottery target. In the Build Settings tab, find the setting for Objective-C Automatic Reference Counting. Change its value to No (Figure 3.15).

Figure 3.15.

Figure 3.15. Disable Automatic Reference Counting

Now analyze the lottery application. In the Product menu, click Analyze. In the issues navigator, you will see several issues found by the static analyzer; select one and drill down in the tree to examine the analyzer’s thought process (Figure 3.16).

Figure 3.16.

Figure 3.16. The Static Analyzer at Work

In this case, the static analyzer has found a number of memory-related problems in our program because we disabled a feature called automatic reference counting, which we will discuss in the next chapter. This is one of the more useful aspects of the static analyzer: It knows the rules for retain-count memory management in Objective-C, and it can also identify other dangerous patterns in your code.

Leave automatic reference counting disabled for now.

  • + Share This
  • 🔖 Save To Your Account