Information Technology Reference
In-Depth Information
description isn't always listed in class reference documentation. The easiest way to discover what this method
does is by experimenting.
TIP
Every message in the console appears with a date and time stamp. The time is specified to the nearest millisecond.
You can sometimes use this information to check performance.
Using custom messages
You can add custom messages anywhere in your code. Figure 15.5 shows a log message added to the applic-
ation didFinishLaunchingWithOptions: method in the app delegate of a typical iOS project. This
method runs once when the application loads. The console logs the message, as shown in the figure.
You can use this technique to log events—for example, by writing a message to confirm when a method is
called. You also can use messages to list variables and values, display loop counts, and so on.
Unlike breakpoints, messages don't interrupt execution. They're usually faster and more informative than break-
points when you want to check a sequence of events, but they don't give you the option of exploring memory
or checking conditional execution.
CAUTION
Be careful about using messages in loops. There's nothing to stop you writing from a message on every repeat of a
loop, but this may generate hundreds or thousands of messages. It's good practice to use messages more select-
ively. Don't forget that you can surround log messages with custom code to create conditional logging. For ex-
ample, you might log only every tenth or hundredth iteration of a loop.
CAUTION
NSLog is slow, and you certainly don't want to include it in production code. You can comment out all NSLog
statements by hand, but it's easier to include a single line with #define NSLog in the project's .pch file. This re-
defines NSLog to a null feature and eliminates it from the project. You can comment out this line to re-enable log-
ging if you need to continue debugging.
FIGURE 15.5
Adding an NSLog message to send output to the console
Search WWH ::




Custom Search