Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of Ruby method_missing in Objective C / iOS

As far as I understand Objective C / iOS is very dynamic language, so is it as dynamic as Ruby ? Does it have the equivalent of method_missing ? I asked the same question for js Does Javascript have something like Ruby's method_missing feature? and was disappointed that js does not support it yet.

like image 987
user310291 Avatar asked Mar 21 '12 07:03

user310291


1 Answers

Objective-C is dynamic, although having been a Ruby programmer, I would say it is not quite as dynamic as Ruby.

Objective-C does have an equivalent of method_missing. You'll want to override both forwardInvocation: and methodSignatureForSelector: and follow this important advice from Apple:

Important To respond to methods that your object does not itself recognize, you must override methodSignatureForSelector: in addition to forwardInvocation:. The mechanism for forwarding messages uses information obtained from methodSignatureForSelector: to create the NSInvocation object to be forwarded. Your overriding method must provide an appropriate method signature for the given selector, either by preformulating one or by asking another object for one.

Don't use doesNotRecognizeSelector: as Apple warns that it must always result in an exception being thrown.

Please see the NSObject class documentation for further details: http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html

like image 171
louielouie Avatar answered Oct 11 '22 18:10

louielouie