Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message Forwarding in Objective C

Can anyone give a brief explanation of how to use message forwarding?

Links

  • Apple documentation: Apple documentation tends to be good as a reference, but lengthy enough to not be the best as an introduction.
like image 231
Casebash Avatar asked Jan 22 '23 21:01

Casebash


1 Answers

Simple delegation pattern: your object responds to the message aMethod, then it check if some other object responds to the message aMethod by sending [otherObject respondsToSelector:@selector(aMethod)], which returns a bool. If otherObject does, you're all clear to send the message.

More technical goodness NSInvocation method: if your object is sent a message it can't respond to (crazyMethodName), then forwardInvocation is called on your object. The default implementation of forwardInvocation for NSObject just calls doesNotRecognizeSelector because, well, your object doesn't recognize the selector. You can override the default implementation of forwardInvocation by checking if another object responds to the selector of the invocation, and invoking that invocation on the other object if so.

like image 78
refulgentis Avatar answered Feb 04 '23 22:02

refulgentis