Skip to content

Softshag/NSInvocation-blocks

 
 

Repository files navigation

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.

About

Even-easier NSInvocation creation thanks to blocks

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 100.0%