Information Technology Reference
In-Depth Information
Creating custom messages
You can write a message to the console by including a call to
printf
anywhere in your code. For example,
printf(“This is a message”);
writes
This is a message
to the console. All of the standard
printf
formatting features are supported.
If you are writing Objective-C rather than C or C++, the
NSLog
function is more comprehensive. It includes
additional formatting and output options that aren't available in
printf,
at the cost of slightly clumsier syn-
tax. For example, to write a text string, you must prefix it with Objective-C's
@
“objectification” feature:
NSLog(@”This is a message”);
NSLog
supports the standard
printf
formatting features and adds a new one—the
%@
option, which displays
information about an object. Table 15.1 summarizes the most useful options.
For example, to display the value of an integer use:
NSLog(@”Int value is: %i”, someInt);
To dump information about an object, use:
NSLog(@”%@”, someObject);
Note that the first
@
prefixes the format and output string; the second
@
selects the format.
This object logging feature has special properties. It runs a method called
description
on the object being
logged. Different objects implement
description
in different ways. For example, data collection objects
such as
NSArray
and
NSDictionary
dump their contents as text. For other objects,
description
de-
faults to the object's class name and memory address.
NOTE