Can someone offer a concrete reason for using an NSInvocation
instead of just using a Block, or even a regular method call? I am finding descriptions of NSInvocation
around the Web, but no examples of where it is vital to use or even the only good option.

- 63,694
- 13
- 151
- 195

- 33
- 1
- 7
-
NSInvocation can't capture values from the enclosing scope as easily as blocks can. – David Zech Apr 23 '14 at 20:28
-
Can you add something about why you're even thinking of using them interchangeably? That would help answerers focus on your actual problem. – jscs Apr 23 '14 at 20:32
-
3FYI, you very rarely create NSInvocations. They tend only to be used in low-level plumbing and things like `forwardInvocation:`. Blocks are much more widely used. – Chuck Apr 23 '14 at 21:30
-
Hi, actually this came up in an interview at a well known travel website company. An arrogant dude acted as if they were really important, and how dare I not know why I'd use them. – user255468 Apr 24 '14 at 00:18
-
Note also that NSInvocations are a huge syntactic pain in the butt. They're nontrivial to correctly create and use. – Ben Zotto Apr 25 '14 at 09:12
2 Answers
NSInvocations and blocks can both be used to encapsulate a method call, along with arguments and a receiver that aren't defined until runtime. Before blocks existed, NSInvocation was the only way to do this. If that's all you need to do, blocks are a better bet. Uses of NSInvocation are quite rare these days.
However, unlike blocks, which can represent any amount of code along with captured values, an NSInvocation always represents a single method call.
This has some advantages:
Unlike blocks, which are opaque, an NSInvocation can be queried for the selector, the receiver, the arguments and the signature of the invoked method. If you receive an NSInvocation as an argument to a method (as with NSObject's
-forwardInvocation
), you can extract the arguments and call a different method instead.An NSInvocation can be constructed for an arbitrary method call at runtime using
-initWithMethodSignature:
. If you wanted to implement aneval()
function for Objective-C, for example, you'd use NSInvocation to actually invoke the methods.

- 5,453
- 1
- 26
- 32
-
By eval() function do you mean like in the Lisp sense? Again I don't see any real world purpose for this. – user255468 Apr 24 '14 at 00:22
-
https://github.com/ReactiveCocoa/ReactiveCocoa/blob/master/ReactiveCocoaFramework/ReactiveCocoa/NSObject%2BRACLifting.m – Chris Devereux Apr 24 '14 at 09:16
-
Or if you were writing a mocking library, (1) would be very useful. – Chris Devereux Apr 24 '14 at 09:19
NSInvocation
allows you to dynamically at runtime construct and invoke an Objective-C message call, to a method that you may not know at compile-time, and it allows you to set the arguments using the information about the types and number of arguments from the signature of the method which again you may not know at runtime. (You may have a place that needs to be able to call different methods with different signatures, and pass different things depending on those signatures.) Thus it adds a level of dynamic-ness to the message passing mechanism. I am not sure how blocks relate to this.
NSInvocation
is also used in -forwardInvocation:
if you want your object to be able to handle arbitrary (not fixed at compile-time) messages, where the message dispatch system constructs an NSInvocation
object describing the call and pass it to you, so that you can introspect and potentially modify the arguments and invoke it on another object or otherwise handle the information. This adds a lot of dynamic-ness in handling of messages at runtime. I also don't know how blocks relate to this.

- 119,665
- 29
- 163
- 224