Graphics Programs Reference
In-Depth Information
Static Analyzer
When you build an application, you can ask
Xcode
to analyze your code. The static analyz-
er then makes educated guesses about what would happen if that code were to be executed
and informs you of potential problems. It does this without executing the code.
When the static analyzer checks the code, it examines each function and method individu-
ally by iterating over every possible
code path
. A method can have a number of control
statements (
if
,
for
,
switch
, etc.). The conditions of these statements will dictate which
code is actually executed. A code path is one of the possible paths the code will take given
these control statements. For example, a method that has a single
if
statement has two
code paths: one if the condition fails and one if the condition succeeds.
Open
TouchTracker.xcodeproj
.
Right now,
TouchTracker
doesn't have any code that offends the static analyzer. So, we
will introduce some code that does. In
TouchDrawView.m
, implement the following
method.
- (int)numberOfLines
{
int count;
// Check that they are non-nil before we add their counts...
if (linesInProcess && completeLines)
count = [linesInProcess count] + [completeLines count];
return count;
}
To start the static analyzer, click and hold the
Run
button in the top-left corner of the work-
space. In the pop-up window that appears, choose
Analyze
(
Figure 21.1
). Alternatively,
you can use the keyboard shortcut: Command-Shift-B.
Figure 21.1 Using the static analyzer