Objective C Forward Invocation

The other day I was messing around with Objective C trying to learn about forward invocation.

- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ( [myFriend respondsToSelector: [anInvocation selector]] ) {
[anInvocation invokeWithTarget: myFriend];
}
else {
[super forwardInvocation:anInvocation];
}
}

But for whatever reason I would get a “doesnotrecognizeselector” error when I sent a message to my object that it was supposed to forward.

To fix this you need to also include this.

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
NSMethodSignature *sig;
sig = [super methodSignatureForSelector:aSelector];
if (sig) return sig;
sig = [myFriend methodSignatureForSelector:aSelector];
return sig;
}

This method is used to create the method signature object so you basically just need to make sure and include it whenever you override forwardInvocation.

You can leave a response, or trackback from your own site.

Leave a Reply

Subscribe to RSS Feed Follow me on Twitter!