I’m writing a logger class (who doesn’t ?) with a method
- (void) logWithTag:(NSString *)aTag andMessage:(NSString *)aMsg;
I wish to be able to forward undefined messages to this method. For example :
[myLogger logFoo:@"bar"] should call [myLogger logWithTag:@"Foo" andMessage:@"bar"]
and more generically
[myLogger logXXX:@"bar"] should call [myLogger logWithTag:@"XXX" andMessage:@"bar"]
I’ve looked at resolveInstanceMethod and message forwarding, but from what I understood, they all expect the method to have the same signature.
Any idea ?
thanks !
Apple covers this in the
Objective-C Runtime Programming Guide. Basically you need to overriderespondsToSelector:and return YES if you would like to support the method. Then also overrideforwardInvocation:and then capture the selector string, remove the log prefix, and create a newNSInvocationthat will calllogWithTag:andMessage: