Created
April 16, 2012 11:03
-
-
Save BenedictC/2397777 to your computer and use it in GitHub Desktop.
NSTimer+Block category. Schedule a timer and use a block as the callback.
This file contains hidden or 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
@interface NSTimer (EMKTimerBlocks) | |
+(id)EMK_scheduleTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void(^)(NSTimer *))block; | |
@end | |
@implementation NSTimer (EMKTimerBlocks) | |
+(id)EMK_scheduleTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void(^)(NSTimer *))block | |
{ | |
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[block copy] forKey:@"block"]; | |
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(EMK_executeTimerBlock:) userInfo:userInfo repeats:repeats]; | |
return timer; | |
} | |
+(void)EMK_executeTimerBlock:(NSTimer *)theTimer | |
{ | |
void (^block)(NSTimer *) = [[theTimer userInfo] objectForKey:@"block"]; | |
block(theTimer); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't [block copy] be [[block copy] autorelease](or release after adding to dictionary)? Seems like you'd leak the reference.