Archive for the ‘Objective-C’ Category

Simple “method got called” debugging

Thursday, February 26th, 2009

For quick and dirty project debugging, I often find myself using code like:

- (void)someRandomMethod
{
NSLog(@"-someRandomMethod called");
// ...
}

Playing around with precompiled headers in XCode, it occurred to me there’s a much neater way to do this. In the precompiled header (XCode probably created a .pch file for you) add:

#define HELLO NSLog(@"-----TRACE----- %s (%s:%d)", __PRETTY_FUNTION__, __FILE__, __LINE__);

Then in any method in your project you can get easy tracing:

- (void)someOtherMethod
{
HELLO
// ...
}

To prevent trace messages ever showing up in release builds, you could wrap the definition in an #ifdef and make the macro effectively a no-op.

Tracing Objective-C method calls

Friday, January 2nd, 2009

There are probably more clever ways to do this, but as a quick and dirty hack I find this useful for finding out which methods are being, or could potentially be, called on my object:

- (BOOL)respondsToSelector:(SEL)aSelector
{
  NSLog(@"respondsToSelector:'%s'", sel_getName(aSelector));
  return [super respondsToSelector:aSelector];
}