Recipe: NSLog Tricks

NSLog is not just useful for printing out your variables in the heat of a debugging session – for instance, did you know you can get NSLog to spill the beans on the line number you’re on in the current method, or the path of the current file?  There are a number of compile-time macros that  you can use in your NSLog statements to help you see more about your code:

For example, to see which method and line number you’re in, try this:

NSLog(@”Greetings from  %@ at line %d”, NSStringFromSelector(_cmd), __LINE__)

which will yield something like this in your console at run time:

2010-02-11 06:44:34.325 BarsReport[4218:207] Greetings from recenterMapOnUser: at line 189

Macro Format Specifier Description
NSStringFromSelector(_cmd) %@ Name of the current selector
NSStringFromClass([self class]) %@ Name of the current object’s class
__FUNCTION__ %s Current function signature
__FILE__ %s Path of the current file
__LINE__ %d Current line number
__PRETTY_FUNCTION__ %s Complete current function signature (includes arguments).

Source: Apple Developer Documentation

VN:F [1.9.1_1087]
Rating: 0.0/10 (0 votes cast)
Twitter Digg Delicious Stumbleupon Technorati Facebook

No comments yet... Be the first to leave a reply!

Leave a Reply