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.
3 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

- 14,881
- 3
- 26
- 31
-
1Also see the section on [Message Forwarding](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtForwarding.html#//apple_ref/doc/uid/TP40008048-CH105) in the Objective-C Runtime Guide. – Kurt Revis Mar 21 '12 at 07:52
I've implemented this pattern based on the example of adding the same methods to a UITextField
and a UITextView
subclass: https://stackoverflow.com/a/19661059/171933
Enjoy!

- 1
- 1

- 42,912
- 19
- 126
- 165
I guess method_missing is a callback method that gets called when an object is missing a certain method. Well, you can use respondsToSelector method to check if an object can respond to a method. It's a method declared in NSObject protocol. So instead of getting method_missing called after realizing that some method is missing you can check it beforehands.
if ([myObject respondsToSelector:@selector(someMethodThatMightBeMissing:)])
NSLog(@"The method is missing");

- 12,117
- 26
- 122
- 206
-
`respondsToSelector:` in Objective-C corresponds to `respond_to?` in Ruby, not `method_missing`. – louielouie Mar 21 '12 at 07:46
-
Yours seems simpler than louielouie what is the difference because i'm not able to judge as newbie thanks :) – user310291 Mar 21 '12 at 07:47
-
The one I suggested helps you check if an object can respond to a method. You'll mostly want to use this method when you suspect an object might not respond to a method, it's kind of a precaution. If an object or its predecessor(s) do not have such method, your program will crash. But calling a method only with the condition that the object responds to it'll prevent your program from crashing. method_missing method as far as I understand is a method that's called when such a situation happens and doesn't let your program crash.So the former is for cheking and the latter is for handling a case. – Mikayil Abdullayev Mar 21 '12 at 08:41
-
I think @louielouie's answer is more accurate and appropriate as he knows Ruby but I have no idea about it – Mikayil Abdullayev Mar 21 '12 at 08:45