Skip to content

Instantly share code, notes, and snippets.

@bsneed
Last active December 17, 2015 17:29
Show Gist options
  • Select an option

  • Save bsneed/5646807 to your computer and use it in GitHub Desktop.

Select an option

Save bsneed/5646807 to your computer and use it in GitHub Desktop.
rehash of callSelector
- (void)makeObjectsPerformSelector:(SEL)aSelector argumentAddresses:(void *)arg1, ...
{
#define kMaximumCallSelectorArguments 20
// if there's nothing in here, GTFO.
if (self.count == 0)
return;
id sampleTarget = [self objectAtIndex:0];
NSMethodSignature *methodSig = [[sampleTarget class] instanceMethodSignatureForSelector:aSelector];
NSUInteger numberOfArguments = [methodSig numberOfArguments] - 2;
if (numberOfArguments >= kMaximumCallSelectorArguments)
[NSException raise:@"SDException" format:@"makeObjectsPerformSelector:argumentAddresses: cannot take more than %i arguments.", kMaximumCallSelectorArguments];
void *arguments[kMaximumCallSelectorArguments];
memset(arguments, 0, sizeof(void *) * kMaximumCallSelectorArguments);
va_list args;
va_start(args, arg1);
arguments[0] = arg1;
for (NSUInteger i = 1; i < numberOfArguments; i++)
arguments[i] = va_arg(args, void *);
va_end(args);
NSArray *copyOfSelf = [self copy];
for (NSObject *object in copyOfSelf)
if([object respondsToSelector:aSelector])
{
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: methodSig];
[invocation setTarget:object];
[invocation setSelector:aSelector];
void *theArg = nil;
for (NSUInteger i = 0; i < numberOfArguments; i++)
{
theArg = arguments[i];
[invocation setArgument:theArg atIndex:i + 2];
}
[invocation invoke];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment