Last Updated: February 25, 2016
· mokagio

NSInvocation with non object parameter

Objective-C and Foundation give us the ability to do pretty awesome things by building method calls at run time.

As long as you are in the objects realm everything's straightforward, but if you happen to need using non objects there are some extra steps to be taken.

Here's how to build an NSInvocation for a method that has a CGFloat parameter.

// Build the NSInvocation
SEL selector = ...;
NSMethodSignature *signature = [someObject methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

// Configure the NSInvocation instance
invocation.selector = selector; = ...;

// Using a non object parameter is easy!
CGFloat floatValue = 42.0;
[invocation setArgument:&floatValue atIndex:2];

You can find out more about how powerful and versatile NSInvocation is on the Apple Documentation.