Capturing all methods/message calls on an object
cocoa, nsobject, objective-c
Solution
You can also use objective-c forwarding. Basically you can create a proxy object that logs the methods then forwards the call to the original. See my blog post for more details.
@interface LoggerProxy : NSObject
{
id original;
}
- (id)initWithOriginal:(id) value;
@end
@implementation LoggerProxy
- (id) initWithOriginal:(id)value
{
if (self = [super init]) {
original = value;
}
return self;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
NSMethodSignature *sig = [super methodSignatureForSelector:sel];
if(!sig)
{
sig = [original methodSignatureForSelector:sel];
}
return sig;
}
- (void)forwardInvocation:(NSInvocation *)inv
{
NSLog(@"[%@ %@] %@ %@", original, inv,[inv methodSignature],
NSStringFromSelector([inv selector]));
[inv invokeWithTarget:original];
}
@end
Problem
How do I put a "hook" on an object so I can see what messages are being sent to it? (ie do an NSLog() every time a message is sent to an object). I think recall seeing this done before but I forget how. I am thinking it might help me track down why part of my code is not working.