Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Even-easier NSInvocation creation thanks to blocks

branch: master
README.markdown

NSInvocation+blocks

Inspired by Jonathan Wight's CInvocationGrabber and Dave Dribin's DDInvocationGrabber, I realized Objective-C's newish support for blocks could make generating NSInvocations even easier.

Let's have a bake-off!

Manual:

NSInvocation *theInvocation = [NSInvocation invocationWithMethodSignature:[theString methodSignatureForSelector:@selector(insertString:atIndex:)]];
[theInvocation setSelector:@selector(insertString:atIndex:)];
[theInvocation setTarget:theString];
NSString *theFirstArgument = "Hello World";
[theInvocation setArgument:&theFirstArgument atIndex:2];
unsigned theSecondArgument = 42;
[theInvocation setArgument:&theFirstArgument atIndex:3];

Ick.

CInvocationGrabber:

CInvocationGrabber *theGrabber = [CInvocationGrabber invocationGrabber];
[[theGrabber prepareWithInvocationTarget:theString] insertString:@"Hello World" atIndex:42];
NSInvocation *theInvocation = [theGrabber invocation];

Much nicer.

NSInvocation+blocks:

NSInvocation *theInvocation = [NSInvocation jr_invocationWithTarget:theString block:^(id theString){
    [theString insertString:@"Hello World" atIndex:42];
}];

Butter.

Something went wrong with that request. Please try again.