Game Development Reference
In-Depth Information
The NSLog function takes the same format strings as NSString . This allows you to
print out any variable along with the message. Listing 14-2 shows how you can log a vari-
ety of basic data types.
Listing 14-2 . Logging basic data types with format strings
int intValue = 123;
NSUInteger unsignedIntegerValue = 456789;
CGFloat floatValue = 0.123456789;
NSLog(@"intValue=%i integerValue=%lu floatValue=%f
floatValue.2=%.2f",
intValue, unsignedIntegerValue, floatValue,
floatValue);
The resulting log string looks like this:
intValue=123 integerValue=456789 floatValue=0.123457
floatValue.2=0.12
The most commonly used format identifiers are %i and %u for int and unsigned
int values, %f and %.1f for floating-point values. The latter specifies that only the first
three digits following the decimal separator should be printed. This helps readability be-
cause logging positions as 237.352349x288.927534 is a lot less readable than
237.4x288.9—especially, if representing the exact floating-point value is of little signific-
ance.
Sometimes you may see %d being used, which is simply an alias for %i . And if you want
to log NSInteger or NSUInteger , you have to use the format specifiers for long in-
tegers %li and %lu , as well as cast the NSInteger / NSUInteger value to ( long ) and
( unsigned long ), respectively.
Caution If you get a compiler warning on an NSLog line that indicates you are
not using the correct format specifier or that the value will be truncated, you
should fix that problem. Otherwise, the logged value may be different from the
actual value, which is going to be a source of confusion.
Listing 14-3 shows you how to log Objective-C class instances, also known as objects .
Search WWH ::




Custom Search