Skip to content

Instantly share code, notes, and snippets.

@plantpurecode
Created November 5, 2011 00:44
Show Gist options
  • Save plantpurecode/1340889 to your computer and use it in GitHub Desktop.
Save plantpurecode/1340889 to your computer and use it in GitHub Desktop.
Birthday celebration
//GCD way:
double delayInSeconds = (24 * 60) * 60;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self setAge:[self age] + 1];
});
//NSInvocation way:
//We can't simply call [self performSelector:@selector(setAge:) withObject:[self age] + 1 afterDelay:(24 * 60) * 60]
//because the withObject: argument only accepts an "id" - le sigh.
NSMethodSignature *signature = [self methodSignatureForSelector:@selector(setAge:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:signature];
char newAge = [self age] + 1; //we don't live more than 127 years, do we?
[inv setArgument:&newAge
atIndex:0];
[inv performSelector:@selector(invokeWithTarget:) withObject:self afterDelay:(24 * 60) * 60];
//Happy birthday to me! :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment