Skip to content

Instantly share code, notes, and snippets.

@justin
Created September 9, 2011 15:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save justin/1206549 to your computer and use it in GitHub Desktop.
Save justin/1206549 to your computer and use it in GitHub Desktop.
NSTimer+Blocks
typedef void (^SGBlock)();
@interface NSTimer (Blocks)
+ (id)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)shouldRepeat block:(SGBlock)block;
+ (id)timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)shouldRepeat block:(SGBlock)block;
@end
@implementation NSTimer (Blocks)
+ (id)scheduledTimerWithTimeInterval:(NSTimeInterval)theInterval repeats:(BOOL)shouldRepeat block:(SGBlock)theBlock
{
SGBlock block = [theBlock copy];
id ret = [self scheduledTimerWithTimeInterval:theInterval target:self selector:@selector(sg_fireBlock:) userInfo:block repeats:shouldRepeat];
[block release];
return ret;
}
+ (id)timerWithTimeInterval:(NSTimeInterval)theInterval repeats:(BOOL)shouldRepeat block:(SGBlock)theBlock
{
SGBlock block = [theBlock copy];
id ret = [self timerWithTimeInterval:theInterval target:self selector:@selector(sg_fireBlock:) userInfo:block repeats:shouldRepeat];
[block release];
return ret;
}
+ (void)sg_fireBlock:(NSTimer *)theTimer;
{
if([theTimer userInfo])
{
SGBlock block = (SGBlock)[theTimer userInfo];
block();
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment