Created
November 5, 2011 00:44
-
-
Save plantpurecode/1340889 to your computer and use it in GitHub Desktop.
Birthday celebration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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