Skip to content

Instantly share code, notes, and snippets.

@jonathanlking
Created August 19, 2012 15:00
Show Gist options
  • Save jonathanlking/3395332 to your computer and use it in GitHub Desktop.
Save jonathanlking/3395332 to your computer and use it in GitHub Desktop.
NSInvocation Example
//
// ViewController.m
//
//
// Created by Jonathan King on 19/08/2012.
//
//
#import "ViewController.h"
// Makes inserting arguments at an index easier to read and manage
#define NSInvocationObjectIndex(index) index + 2
@interface ViewController ()
// Private Methods
- (void)setUpInvocations;
- (void)logThisString:(NSString *)string andThisInteger:(int)integer;
// Private Properties
@property (strong, nonatomic) NSMutableArray *mutableArray;
@end
@implementation ViewController
// Not necessary with LLVM 4.0
@synthesize mutableArray = _mutableArray;
#pragma mark View Lifecycle
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setUpInvocations];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
#pragma mark Instace Methods
- (void)setUpInvocations
{
if (!_mutableArray) _mutableArray = [[NSMutableArray alloc] init];
// Set up the NSInvocation(s)
NSMethodSignature *loggingStringAndIntegerMethodSignature = [self methodSignatureForSelector:@selector(logThisString:andThisInteger:)];
NSInvocation *logStringAndInteger = [NSInvocation invocationWithMethodSignature:loggingStringAndIntegerMethodSignature];
logStringAndInteger.selector = @selector(logThisString:andThisInteger:);
logStringAndInteger.target = self;
NSMethodSignature *addingObjectToArrayMethodSignature = [_mutableArray methodSignatureForSelector:@selector(addObject:)];
NSInvocation *addObjectToArray = [NSInvocation invocationWithMethodSignature:addingObjectToArrayMethodSignature];
addObjectToArray.selector = @selector(addObject:);
addObjectToArray.target = _mutableArray;
// Variables that will be included in the invocations
NSString *stringThatWillBeLogged = @"Twenty Five";
int integerThatWillBeLogged = 5;
NSString *stringthatWillBeAddedToTheArray = @"Array String";
// Set the arguments of the NSInvocation(s)
[logStringAndInteger setArgument:&stringThatWillBeLogged atIndex:NSInvocationObjectIndex(0)];
[logStringAndInteger setArgument:&integerThatWillBeLogged atIndex:NSInvocationObjectIndex(1)];
[addObjectToArray setArgument:&stringthatWillBeAddedToTheArray atIndex:NSInvocationObjectIndex(0)];
// Invoke and schedule the NSInvocation(s)
[logStringAndInteger invoke];
[NSTimer scheduledTimerWithTimeInterval:2.0f invocation:logStringAndInteger repeats:YES];
[addObjectToArray invoke];
[NSTimer scheduledTimerWithTimeInterval:2.0f invocation:addObjectToArray repeats:YES];
}
- (void)logThisString:(NSString *)string andThisInteger:(int)integer
{
NSLog(@"String: %@ \n Integer: %d \n \n", string, integer);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment