April 13, 2016

Method Forwarding in Objective-C Pitfall

A common pitfall of trying to use message forwarding in Objective-C is forgetting to implement

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector

in addition to

- (void)forwardInvocation:(NSInvocation *)anInvocation

Another problem is that sometimes calling objects will use

- (BOOL)respondsToSelector:(SEL)aSelector

to determine if your proxy object can respond to a particular selector. The problem is that even if you forward that selector, because the message isn't actually sent, the default implementation of respondsToSelector will return NO, causing the forwarding to fail. Therefore you should also override respondsToSelector.