NSLog
last edited September 7, 2009 15:34:37 (67.188.69.68)
| |
|---|---|
| Edit / History / New / Search | Quick Links: Home / Recent Changes / Glossary / Jobs / Forums / Help |
|
NSLog is a FoundationKit function for printing debug statements to the console. It is defined in NSObjCRuntime?.h:
NSLog works basically like:
But its format string is a NSString object rather than simple char *. You can use it in the predictable ways:
NSLog will also display meaningful values for well known objects as well:
NSLog works in this case by asking each object for a string that describes itself, sending the -description method to the objects. (Note: if an object doesn't implement the description method, you tend to get are result like this <classname : address>. See UsingTheDescriptionMethod
Just to clarify, NSLog understands all the conversion code specifiers that printf and company understand with the addition of '@' which is the conversion code for objects. And remember NSString is an object so don't try to convert one in NSLog via 's' which is the C string specifier.
The single exception to "all the...specifiers" above seems to be the %.*s specifier many people use to print variable length strings.
NSLog is very nice, but be careful which specifier you use. Consider the following:
i (an int) is not an object! So you can't send True, but you could make it one with NSNumber, if you wanted to, like so.
NSLog format specifiers:
"Object" refers to a Cocoa object (responding to -description) or a Core Foundation object. For more, see
Alternatives to NSLog: XLog is an alternative if you think the header is to wide or you would like to log the elapsed time from the previous log. ZNLog? http://codebeach.org/code/show/44 LibComponentLogging is a small library for Objective-C applications on Mac OS X and the iPhone OS which provides log levels, log components for identifying different parts of an application, and an active log level for each log component in order to enable/disable logging for certain parts of an application. Additionally, different logging backends are available, e.g. one which writes to a rotating log file, and one which sends log messages to the Apple System Log facility (ASL). See http://0xc0.de/LibComponentLogging for details. BTTimeLog? is a simple Objective-C class I put together that logs the time used by an application. The output includes a logging level, accumulated and differential times, the class and method that made the call, and a user string with arguments. More at http://steveweller.com/articles/speedup/
See NSLogToFile for redirecting NSLog output.
I modified the example such that i is not equal to 0; in that case (on 32-bit Mac OS X, anyway) NSLog would interpret it as the nil object and would not crash. It's still bad form, but the example predicts incorrect behavior. Do not log a string that you're unsure of its content (for example from the network) as the first argument (format string). Rather, use NSLog(@"%@", str) | |
| Edit / History / New / Search | Quick Links: Home / Recent Changes / Glossary / Jobs / Forums / Help |