Skip to content

Instantly share code, notes, and snippets.

@alvesjtiago
Created December 25, 2013 12:54
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save alvesjtiago/8123006 to your computer and use it in GitHub Desktop.
Save alvesjtiago/8123006 to your computer and use it in GitHub Desktop.
Create invocations the simple way. Extracted from ITActionManager.
//
// NSInvocation+SimpleCreation.h
// MAPI
//
// Created by Tiago Alves on 08/12/13.
// Copyright (c) 2013 Iterar. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSInvocation (SimpleCreation)
+ (NSInvocation*)invocationWithTarget:(id)target andSelector:(SEL)selector;
+ (NSInvocation*)invocationWithTarget:(id)target selector:(SEL)selector andArguments:(NSArray*)arguments;
@end
//
// NSInvocation+SimpleCreation.m
// MAPI
//
// Created by Tiago Alves on 08/12/13.
// Copyright (c) 2013 Iterar. All rights reserved.
//
#import "NSInvocation+SimpleCreation.h"
@implementation NSInvocation (SimpleCreation)
+ (NSInvocation*)invocationWithTarget:(id)target andSelector:(SEL)selector {
NSMethodSignature *sig = [[target class] instanceMethodSignatureForSelector:selector];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
[inv setTarget:target];
[inv setSelector:selector];
return inv;
}
+ (NSInvocation*)invocationWithTarget:(id)target selector:(SEL)selector andArguments:(NSArray *)arguments {
NSInvocation *inv = [NSInvocation invocationWithTarget:target andSelector:selector];
int argumentIndex = 2;
for (int i = 0; i < arguments.count; i++) {
id argument = [arguments objectAtIndex:i];
[inv setArgument:&argument atIndex:argumentIndex];
argumentIndex += 1;
}
[inv retainArguments];
return inv;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment